Search code examples
gtk3wandpyobject

Convert Wand image to GTK image


Context: Debian 11, Python 3.9.2, PyObject GTK 3.0, WAND 0.6.5

I currently use PIL and to load image in GTK I do:

data = pil_img.tobytes()
pixbuf = GdkPixbuf.Pixbuf.new_from_data(data,  ...)

I want to do the same thing with WAND and have tried data = wand_img.make_blob() but it doesn't work, GTK answers "constructor returned NULL"

Thankfully using numpy works: data = numpy.array(wand_img).tobytes()

So what? The problem is that going through numpy is very slow, causing me to lose 400 ms compared with PIL, which makes the application much less responsive.

Is there a way to convert WAND image to bytes without using numpy ?


Solution

  • Try using Image.export_pixels()

    w, h = img.size
    rgb = GdkPixbuf.Colorspace.RGB
    data = img.export_pixels()
    pixbuf = GdkPixbuf.Pixbuf.new_from_data(data, rgb, True, 8, w, h, w*4)
    

    Also look into using ImageMagick's Q8 releases. The 400ms is probably related to 16 bits-per-sample down-scaling to 8.