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

PIL.UnidentifiedImageError: cannot identify image file #4

Closed steven-na closed 12 months ago

steven-na commented 12 months ago

I'm trying to use the example provided in #2 but i don't get expected results,

def get_images(self):
        prompt_id = self.queue_prompt()['prompt_id']
        receiving_fin = False
        while True:
            out = self.ws.recv()
            if isinstance(out, str):
                message = json.loads(out)
                print(message)
                if message['type'] == 'executing':
                    if message['data']['node'] == '22':
                        receiving_fin = True
                    data = message['data']
                    if data['node'] is None and data['prompt_id'] == prompt_id:
                        break #Execution is done
            elif receiving_fin == True:
                if type(out) == bytes:
                    s = struct.calcsize(">II")
                    data = memoryview(out)
                    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
                            im = Image.open(data)
                            im.show()
                receiving_fin = False

The whole output from the WS looks like this:

{'type': 'status', 'data': {'status': {'exec_info': {'queue_remaining': 0}}, 'sid': '54cde4d6-e60b-40c5-8624-860ef07f2b0c'}}
{'type': 'status', 'data': {'status': {'exec_info': {'queue_remaining': 1}}}}
{'type': 'status', 'data': {'status': {'exec_info': {'queue_remaining': 1}}}}
{'type': 'execution_start', 'data': {'prompt_id': '45cb36fa-ff2e-4743-b279-35364f6ac710'}}
{'type': 'execution_cached', 'data': {'nodes': ['20', '6', '17', '19', '10', '7', '4'], 'prompt_id': '45cb36fa-ff2e-4743-b279-35364f6ac710'}}
{'type': 'executing', 'data': {'node': '3', 'prompt_id': '45cb36fa-ff2e-4743-b279-35364f6ac710'}}
{'type': 'progress', 'data': {'value': 1, 'max': 6}}
<class 'bytes'>
{'type': 'progress', 'data': {'value': 2, 'max': 6}}
<class 'bytes'>
{'type': 'progress', 'data': {'value': 3, 'max': 6}}
<class 'bytes'>
{'type': 'progress', 'data': {'value': 4, 'max': 6}}
<class 'bytes'>
{'type': 'progress', 'data': {'value': 5, 'max': 6}}
<class 'bytes'>
{'type': 'progress', 'data': {'value': 6, 'max': 6}}
<class 'bytes'>
{'type': 'executing', 'data': {'node': '8', 'prompt_id': '45cb36fa-ff2e-4743-b279-35364f6ac710'}}
{'type': 'executing', 'data': {'node': '22', 'prompt_id': '45cb36fa-ff2e-4743-b279-35364f6ac710'}}
Traceback (most recent call last):
  File "C:\Users\blake\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py", line 3240, in open
    fp.seek(0)
AttributeError: 'memoryview' object has no attribute 'seek'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Programming\comfyuiapi\main.py", line 36, in <module>
    a.get_images()
  File "C:\Programming\comfyuiapi\api.py", line 51, in get_images
    im = Image.open(data)
  File "C:\Users\blake\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py", line 3242, in open
    fp = io.BytesIO(fp.read())
AttributeError: 'memoryview' object has no attribute 'read'
steven-na commented 12 months ago

I've tried other combinations like

print(BytesIO(base64.b64decode(out)))
im = Image.open(BytesIO(base64.b64decode(out)))
im.show()

but i get the similar errors like:

Traceback (most recent call last):
  File "C:\Programming\comfyuiapi\main.py", line 36, in <module>
    a.get_images()
  File "C:\Programming\comfyuiapi\api.py", line 44, in get_images
    im = Image.open(BytesIO(base64.b64decode(out)))
  File "C:\Users\blake\AppData\Local\Programs\Python\Python310\lib\site-packages\PIL\Image.py", line 3298, in open
    raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000001EBC8B4BA60>
Acly commented 12 months ago

I think you want

Image.open(BytesIO(data[s:]))
steven-na commented 12 months ago

I think you want

Image.open(BytesIO(data[s:]))
  • The [s:] strips the "event" and "format" codes, which don't belong to the image
  • Don't use base64 - it's raw bytes of a PNG

Thank you very much, this worked