Markdown to html with a twist

Port of: https://mahotas.readthedocs.io/en/latest/wally.html

Code: Copy
Dim pylab As PyWrapper = Py.ImportModule("pylab")
Dim mahotas As PyWrapper = Py.ImportModule("mahotas")
Dim demos As PyWrapper = Py.ImportModule("mahotas.demos")
Dim np As PyWrapper = Py.ImportModule("numpy")

Dim wally As PyWrapper = demos.Run("load").Arg("Wally")
Dim wfloat As PyWrapper = wally.Run("astype").Arg("float")
Dim rgb As PyWrapper = wfloat.Run("transpose").Arg(Array(2, 0, 1))
Dim w As PyWrapper = wfloat.Run("mean").Arg(2)
Dim pattern As PyWrapper = np.Run("ones").Arg(Array(24, 16)).Arg("float")
For i = 0 To 1
    pattern.Set(Py.Slice(i, Null).Arg(4), -1)
Next
Dim v As PyWrapper = mahotas.Run("convolve").Arg(rgb.Get(0).OprSub(w)).Arg(pattern)
Dim mask As PyWrapper = v.OprEqual(v.Run("max"))
mask = mahotas.Run("dilate").Arg(mask).Arg(np.Run("ones").Arg(Array(48, 24)))
Dim maskWithAdditionalDim As PyWrapper = mask.Get3D(Py.SliceAll, Py.SliceAll, Null)
Dim notmask As PyWrapper = wally.OprMul(0.9).OprMul(maskWithAdditionalDim.OprNot)
Dim maskedWally As PyWrapper = wally.Run("copy")
np.Run("subtract").Arg(wally).Arg(notmask).ArgNamed("casting", "unsafe") _
.ArgNamed("out", maskedWally)

Dim imgs As List = Array(wally, maskedWally)
For i = 0 To imgs.Size - 1
    pylab.Run("subplot").Arg(1).Arg(2).Arg(i + 1)
    pylab.Run("imshow").Arg(imgs.Get(i))
    pylab.Run("axis").Arg("off")
Next

pylab.Run("show")
Clipboard Image.jpg


In this case it would have been simpler to create a sub with the python code:
Code: Copy
Private Sub FindWally (img As Object) As PyWrapper
    Dim Code As String = $"
import mahotas
import numpy as np
def FindWally (img):
    wally = img.copy()
    wfloat = wally.astype(float)
    r,g,b = wfloat.transpose((2,0,1))
    w = wfloat.mean(2)
    pattern = np.ones((24,16), float)
    for i in range(2):
        pattern[i::4] = -1
    v = mahotas.convolve(r-w, pattern)
    mask = (v == v.max())
    mask = mahotas.dilate(mask, np.ones((48,24)))
    np.subtract(wally, .8*wally * ~mask[:,:,None], out=wally, casting='unsafe')
    return wally
"$
    Return Py.RunCode("FindWally", Array(img), Code)
End Sub

Usage:
Code: Copy
Dim pylab As PyWrapper = Py.ImportModule("pylab")
Dim demos As PyWrapper = Py.ImportModule("mahotas.demos")

Dim wally As PyWrapper = demos.Run("load").Arg("Wally")
Dim MaskedWally As PyWrapper = FindWally(wally)

Dim imgs As List = Array(wally, MaskedWally)
For i = 0 To imgs.Size - 1
    pylab.Run("subplot").Arg(1).Arg(2).Arg(i + 1)
    pylab.Run("imshow").Arg(imgs.Get(i))
    pylab.Run("axis").Arg("off")
Next

pylab.Run("show")
 
exciting to see what you are cooking for us @Erel 🍳:)

i use this simple but cool python snipped lately that takes a gif and convert it to a sprite sheet (GUI python app).


can it also be used with the new python library??

Python: Copy
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageSequence
import os

def convert_gif_to_spritesheet(source_folder, output_folder, columns=4):
    for filename in os.listdir(source_folder):
        if filename.lower().endswith('.gif'):
            gif_path = os.path.join(source_folder, filename)
            with Image.open(gif_path) as gif:
                frames = [frame.copy() for frame in ImageSequence.Iterator(gif)]
                if frames:
                    frame_width, frame_height = frames[0].size
                    rows = (len(frames) + columns - 1) // columns
                    sheet_width = columns * frame_width
                    sheet_height = rows * frame_height

                    spritesheet = Image.new('RGBA', (sheet_width, sheet_height))

                    for i, frame in enumerate(frames):
                        x = (i % columns) * frame_width
                        y = (i // columns) * frame_height
                        spritesheet.paste(frame, (x, y))

                    output_path = os.path.join(output_folder, filename.replace('.gif', '.png'))
                    spritesheet.save(output_path)
                    print(f"Saved: {output_path}")
 
can it also be used with the new python library??
Just tried it, it runs ok. I got rubbish out as I dont know what it expects as input, but the B4J code to run it is simple (ignoring the start up for PyBridge)
Code: Copy
Sub sprite_cutter(sourceFolder As String, outputFolder As String, columns As Int)
    wait for (Py.RunCode("convert_gif_to_spritesheet",Array(sourceFolder,outputFolder,columns), File.ReadString(File.DirAssets,"sprite_cutter.py")).Fetch) Complete (ret As PyWrapper)
    TextArea1.Text = TextArea2.Text & CRLF & ret.value
    TextArea2.Text = ""
End Sub
 
I removed the first 2 lines (tkinter) as they are not needed as you dont use the gui prt of it. Also the last line was changed from print... to return ... so the B4J app gets the path back.

I take it you give it x images (= columns) and it makes 1 image of them all. I ended up with an image 44k pixels wide lol.
 
Back
Top