How show an image that was generated by python?

I have a piece of python code that generates an image. This works, but now I would like to send that image directly to a TOP.

I can only get it working by saving it first as a PNG file and then loading that file back in. Is there a more direct way to show the image without saving it first?

My code

import qrcode
print("imported qrcode")
img = qrcode.make('https://test.nl')

# this works
img.save("myfile.png")
op('moviefilein1').par.file = "myfile.png"

# but I need something like this
op('moviefilein1').par.file = img

Not sure about how qrcode interface works, but you might be able to leverage the virtual file system: Virtual File System - Derivative

I used vfs and BytesIO

1 Like

Thanks, but I was wondering if you can’t just send any image generated with python, straight into a TOP?
I don’t need to save the image, just display it until the next image is generated in python.

To put it differently:

# generate an image
img = myCoolFunction.generateImage()
# send it to a TOP
op('moviefilein').showImage(img)

Something like this should surely be possible?

It is possible, but the question is if it is neccesarry.
In theory you can convert the pillowImage that is returned from the make-function into a numpyArray.
The scriptTop has a copyNumpyArray method to load the values of the numpy-array directly in to a top.
But the np-array you get from the image is in the wrong format and at that Point I just used the 5 lines of code I already had.

https://docs.derivative.ca/ScriptTOP_Class

Not Working example but the gist:

qr_image = self.qrcode.make(  target,
                              box_size = self.ownerComp.par.Fieldsize.eval(),
                              border = self.ownerComp.par.Bordersize.eval() )
        np_array = numpy.array( qr_image )
        op("script1").copyNumpyArray( np_array )
1 Like

TouchDesigner scriptTOP requires 3 channels in the numpy array. The qrcode library returns an image with only a single luminance channel. So we need to convert the qr_image to RGB before creating a numpy array.

Create a scriptTOP and open its callbacks DAT. Replace its content with the following:

import numpy
import qrcode


def onSetupParameters(scriptOp):
    page = scriptOp.appendCustomPage('QR Code')
    p = page.appendStr('Contents', label='Contents')
    return


def onPulse(par):
    return


def onCook(scriptOp):
    qr_image = qrcode.make(scriptOp.par['Contents'].eval()).convert('RGB')
    np_array = numpy.array(qr_image)
    scriptOp.copyNumpyArray(np_array)
    return

The contents of the qrcode can be supplied via the Contents input in the QR Code tab.

1 Like