Acly / comfyui-tooling-nodes

Nodes for using ComfyUI as a backend for external tools. Send and receive images directly without filesystem upload/download.
GNU General Public License v3.0
319 stars 38 forks source link

Send Image (WebSocket) #2

Closed olegchomp closed 1 year ago

olegchomp commented 1 year ago

Hi! Can you provide code example for decoding image?

Acly commented 1 year ago

I'm using python code like this:

async for msg in websocket:
    if isinstance(msg, bytes):
        s = struct.calcsize(">II")
        data = memoryview(msg)
        if len(data) > s:
            event, format = struct.unpack_from(">II", data)            
            if event == 1 and format == 2: # 1=PREVIEW_IMAGE, 2=PNG, see ComfyUI server.py
                return Image.png_from_bytes(data[s:])

where Image.png_from_bytes does QImage.fromData(data, "PNG") because I use Qt anyway, but you can use similar method with eg. PIL.Image.open

olegchomp commented 1 year ago

Thank you!