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 39 forks source link

Sending information to the loadimage node #5

Closed steven-na closed 1 year ago

steven-na commented 1 year ago

I am curious about how it's recommended to send the data for the image. I've been doing it like so:

with open("test_image.png", "rb") as image_file:
     data = base64.b64encode(image_file.read())

load_image_b64["inputs"]["image"] = data.decode("iso-8859-1")

wf = json.dumps(customize_workflow(img))

This required a change in nodes.py:

def load_image(self, image):
        img = Image.open(BytesIO(base64.b64decode(image.encode('iso-8859-1'))))

But it was the only way i could get the image info through a request because i couldnt encode the image with utf-8 Now, I have changed

with open("test_image.png", "rb") as image_file:
     data = base64.b64encode(image_file.read())

to

img = Image.new("RGB", (512, 512))
data = base64.b64encode(img.tobytes())

I'm sure that this is absolutely the wrong way to do it. I am getting this error when running the new code

PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x000001967A2E72E0>
steven-na commented 1 year ago

All of the image stuff is PIL

steven-na commented 1 year ago

https://jdhao.github.io/2020/03/17/base64_opencv_pil_image_conversion/#pil-image-to-base64 This is a very helpful article for converting images between b64, I switch out

img = Image.new("RGB", (512, 512))
data = base64.b64encode(img.tobytes())

to the article's example and it worked