comfyanonymous / ComfyUI

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface.
https://www.comfy.org/
GNU General Public License v3.0
55.11k stars 5.82k forks source link

After launching ComfyUI for a while, the "Load Image" node cannot identify WebP. #3993

Open laisent opened 3 months ago

laisent commented 3 months ago

Expected Behavior

"Load Image" can support various common formats, such as jpeg, png, webp

Actual Behavior

When loading a webp format image, an error message appears, indicating that the image cannot be recognized. In fact, the image is normal.

[2024-07-10 11:34] got prompt
[2024-07-10 11:34] [rgthree] Using rgthree's optimized recursive execution.
[2024-07-10 11:34] !!! Exception during processing!!! local variable 'x' referenced before assignment
[2024-07-10 11:34] Traceback (most recent call last):
  File "/root/work_py/ComfyUI/node_helpers.py", line 16, in pillow
    x = fn(arg)
  File "/root/.conda/envs/comfyui/lib/python3.10/site-packages/PIL/Image.py", line 3298, in open
    elif hasattr(obj, "tostring"):
PIL.UnidentifiedImageError: cannot identify image file '/root/work_py/ComfyUI/input/abc.webp'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/work_py/ComfyUI/node_helpers.py", line 20, in pillow
    x = fn(arg)
  File "/root/.conda/envs/comfyui/lib/python3.10/site-packages/PIL/Image.py", line 3298, in open
    elif hasattr(obj, "tostring"):
PIL.UnidentifiedImageError: cannot identify image file '/root/work_py/ComfyUI/input/abc.webp'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/work_py/ComfyUI/execution.py", line 151, in recursive_execute
    output_data, output_ui = get_output_data(obj, input_data_all)
  File "/root/work_py/ComfyUI/execution.py", line 81, in get_output_data
    return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
  File "/root/work_py/ComfyUI/execution.py", line 74, in map_node_over_list
    results.append(getattr(obj, func)(**slice_dict(input_data_all, i)))
  File "/root/work_py/ComfyUI/nodes.py", line 1462, in load_image
    img = node_helpers.pillow(Image.open, image_path)
  File "/root/work_py/ComfyUI/node_helpers.py", line 24, in pillow
    return x
UnboundLocalError: local variable 'x' referenced before assignment

[2024-07-10 11:34] Prompt executed in 0.04 seconds

Steps to Reproduce

webp img: workflow: workflow.json

example image

Debug Logs

System Info:
    Operating System: CentOS Linux 7 (Core)
    CPE OS Name: cpe:/o:centos:centos:7
    Kernel: Linux 3.10.0-1160.118.1.el7.x86_64
    Architecture: x86-64
Python Env Info:
    Python Env: Python 3.10.6
    pillow==10.4.0
Python 3.10.6 (main, Oct 24 2022, 16:07:47) [GCC 11.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.25.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from PIL import features
   ...: print (features.check_module('webp'))
True


### Other

When the service is just started, it can parse the Webp format. After running for a while, it cannot parse Webp. Other image formats are normal. After restarting ComfyUI, it returns to normal. The image with the error can be loaded normally by executing PIL.Image.Open("abc.webp") in the terminal.
huchenlei commented 3 months ago

Cannot reproduce the issue. Can you share the input image you used?

laisent commented 3 months ago

It is not possible to reproduce the problem when comfyui started. Generally, after running for a period of time (maybe one day, maybe three days?), all Webp format images cannot be parsed, and other formats such as JPEG and PNG can be loaded normally.

The strange thing is that when the error is reported, IPython can parse Webp. At first, I suspected that it was a problem with the pillow package. This performance is not very similar (Webp can be parsed again after restarting)

attachments: abc.zip Load JPG: image Load Webp: image

IPython

Python 3.10.6 (main, Oct 24 2022, 16:07:47) [GCC 11.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.25.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from PIL import Image
Duplicate of #
In [2]: Image.open("abc.jpg")
Out[2]: <PIL.PngImagePlugin.PngImageFile image mode=RGB size=853x1280>

In [3]: Image.open("abc.webp")
Out[3]: <PIL.WebPImagePlugin.WebPImageFile image mode=RGB size=853x1280>

Cannot reproduce the issue. Can you share the input image you used?

ltdrdata commented 3 months ago

Actually there is a potential error. But we need to understand the reason. (This is the error point. And Image.open is failing. https://github.com/ltdrdata/ComfyUI/blob/ffe0bb0a33a8a8d9a999dafb540558646127d443/nodes.py#L1489 But, we don't the know real reason due to error message is hidden.)

Open the ComfyUI/node_helpers.py file and replace the def pillow... section with the following code. If an error occurs, report the error messages.

import traceback

def pillow(fn, arg):
    prev_value = None
    try:
        x = fn(arg)
    except (OSError, UnidentifiedImageError, ValueError): #PIL issues #4472 and #2445, also fixes ComfyUI issue #3416
        prev_value = ImageFile.LOAD_TRUNCATED_IMAGES
        ImageFile.LOAD_TRUNCATED_IMAGES = True
        try:
            x = fn(arg)
        except Exception as e:
            print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
            print(e)
            traceback.print_exc()

    finally:
        if prev_value is not None:
            ImageFile.LOAD_TRUNCATED_IMAGES = prev_value
        return x
laisent commented 3 months ago

Actually there is a potential error. But we need to understand the reason. (This is the error point. And Image.open is failing. https://github.com/ltdrdata/ComfyUI/blob/ffe0bb0a33a8a8d9a999dafb540558646127d443/nodes.py#L1489 But, we don't the know real reason due to error message is hidden.)

Open the ComfyUI/node_helpers.py file and replace the def pillow... section with the following code. If an error occurs, report the error messages.

import traceback

def pillow(fn, arg):
    prev_value = None
    try:
        x = fn(arg)
    except (OSError, UnidentifiedImageError, ValueError): #PIL issues #4472 and #2445, also fixes ComfyUI issue #3416
        prev_value = ImageFile.LOAD_TRUNCATED_IMAGES
        ImageFile.LOAD_TRUNCATED_IMAGES = True
        try:
            x = fn(arg)
        except Exception as e:
            print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
            print(e)
            traceback.print_exc()

    finally:
        if prev_value is not None:
            ImageFile.LOAD_TRUNCATED_IMAGES = prev_value
        return x

I'm so sorry for the late reply. I'm facing this problem again now. ComfyUI/node_helpers.py

def pillow(fn, arg):
    prev_value = None
    try:
        x = fn(arg)
    except UnidentifiedImageError as e:
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
        print(e)
        traceback.print_exc()

        logging.info(f"webp support is {features.check_module('webp')}")
        tmp = os.path.splitext(arg)

log

[2024-07-22 10:51] 2024-07-22 10:51:21,878- root:462- INFO- got prompt
[2024-07-22 10:51] [rgthree] Using rgthree's optimized recursive execution.
[2024-07-22 10:51] !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
[2024-07-22 10:51] cannot identify image file '/root/work_py/ComfyUI/input/zly.jpeg'
[2024-07-22 10:51] Traceback (most recent call last):
[2024-07-22 10:51]   File "/root/work_py/ComfyUI/node_helpers.py", line 22, in pillow
x = fn(arg)
[2024-07-22 10:51]   File "/root/.conda/envs/comfyui/lib/python3.10/site-packages/PIL/Image.py", line 3298, in open
)
[2024-07-22 10:51] PIL.UnidentifiedImageError: cannot identify image file '/root/work_py/ComfyUI/input/zasd.jpeg'
[2024-07-22 10:51] 2024-07-22 10:51:21,905- root:29- INFO- webp support is True
[2024-07-22 10:51] 2024-07-22 10:51:21,905- root:179- ERROR- !!! Exception during processing!!! local variable 'x' referenced before assignment
[2024-07-22 10:51] 2024-07-22 10:51:21,906- root:180- ERROR- Traceback (most recent call last):
File "/root/work_py/ComfyUI/execution.py", line 151, in recursive_execute
output_data, output_ui = get_output_data(obj, input_data_all)
File "/root/work_py/ComfyUI/execution.py", line 81, in get_output_data
return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
File "/root/work_py/ComfyUI/execution.py", line 74, in map_node_over_list
results.append(getattr(obj, func)(**slice_dict(input_data_all, i)))
File "/root/work_py/ComfyUI/nodes.py", line 1462, in load_image
img = node_helpers.pillow(Image.open, image_path)
File "/root/work_py/ComfyUI/node_helpers.py", line 58, in pillow
return x
UnboundLocalError: local variable 'x' referenced before assignment

[2024-07-22 10:51] 2024-07-22 10:51:21,906- root:129- INFO- Prompt executed in 0.01 seconds
ltdrdata commented 3 months ago

/root/work_py/ComfyUI/input/zly.jpeg

/root/work_py/ComfyUI/input/zly.jpeg /root/work_py/ComfyUI/input/zasd.jpeg

It seems that is not a webp file issue.

Post those .jpeg file please.

laisent commented 3 months ago

/root/work_py/ComfyUI/input/zly.jpeg

/root/work_py/ComfyUI/input/zly.jpeg /root/work_py/ComfyUI/input/zasd.jpeg

It seems that is not a webp file issue.

Post those .jpeg file please.

The file name ends with .jpeg, but it is actually a webp file, and the same is true for other webp files.

ipython

In [1]: from PIL import Image

In [2]: img = Image.open("./zasd.jpeg")

In [3]: img.format
Out[3]: 'WEBP'

ffmpeg

ffmpeg version 7.0 Copyright (c) 2000-2024 the FFmpeg developers
  built with Apple clang version 15.0.0 (clang-1500.3.9.4)
  configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/7.0 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags='-Wl,-ld_classic' --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libharfbuzz --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopenvino --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon
  libavutil      59.  8.100 / 59.  8.100
  libavcodec     61.  3.100 / 61.  3.100
  libavformat    61.  1.100 / 61.  1.100
  libavdevice    61.  1.100 / 61.  1.100
  libavfilter    10.  1.100 / 10.  1.100
  libswscale      8.  1.100 /  8.  1.100
  libswresample   5.  1.100 /  5.  1.100
  libpostproc    58.  1.100 / 58.  1.100
Input #0, webp_pipe, from 'zasd.jpeg':
  Duration: N/A, bitrate: N/A
  Stream #0:0: Video: webp, yuv420p(tv, bt470bg/unknown/unknown), 370x555, 25 fps, 25 tbr, 25 tbn
At least one output file must be specified

file: zasd

ltdrdata commented 3 months ago

/root/work_py/ComfyUI/input/zly.jpeg

/root/work_py/ComfyUI/input/zly.jpeg /root/work_py/ComfyUI/input/zasd.jpeg It seems that is not a webp file issue. Post those .jpeg file please.

The file name ends with .jpeg, but it is actually a webp file, and the same is true for other webp files.

ipython

In [1]: from PIL import Image

In [2]: img = Image.open("./zasd.jpeg")

In [3]: img.format
Out[3]: 'WEBP'

ffmpeg

ffmpeg version 7.0 Copyright (c) 2000-2024 the FFmpeg developers
  built with Apple clang version 15.0.0 (clang-1500.3.9.4)
  configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/7.0 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags='-Wl,-ld_classic' --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libharfbuzz --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopenvino --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon
  libavutil      59.  8.100 / 59.  8.100
  libavcodec     61.  3.100 / 61.  3.100
  libavformat    61.  1.100 / 61.  1.100
  libavdevice    61.  1.100 / 61.  1.100
  libavfilter    10.  1.100 / 10.  1.100
  libswscale      8.  1.100 /  8.  1.100
  libswresample   5.  1.100 /  5.  1.100
  libpostproc    58.  1.100 / 58.  1.100
Input #0, webp_pipe, from 'zasd.jpeg':
  Duration: N/A, bitrate: N/A
  Stream #0:0: Video: webp, yuv420p(tv, bt470bg/unknown/unknown), 370x555, 25 fps, 25 tbr, 25 tbn
At least one output file must be specified

file: zasd

I cannot download that image file.

laisent commented 3 months ago

/root/work_py/ComfyUI/input/zly.jpeg

/root/work_py/ComfyUI/input/zly.jpeg /root/work_py/ComfyUI/input/zasd.jpeg It seems that is not a webp file issue. Post those .jpeg file please.

The file name ends with .jpeg, but it is actually a webp file, and the same is true for other webp files. ipython

In [1]: from PIL import Image

In [2]: img = Image.open("./zasd.jpeg")

In [3]: img.format
Out[3]: 'WEBP'

ffmpeg

ffmpeg version 7.0 Copyright (c) 2000-2024 the FFmpeg developers
  built with Apple clang version 15.0.0 (clang-1500.3.9.4)
  configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/7.0 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags='-Wl,-ld_classic' --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libharfbuzz --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopenvino --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon
  libavutil      59.  8.100 / 59.  8.100
  libavcodec     61.  3.100 / 61.  3.100
  libavformat    61.  1.100 / 61.  1.100
  libavdevice    61.  1.100 / 61.  1.100
  libavfilter    10.  1.100 / 10.  1.100
  libswscale      8.  1.100 /  8.  1.100
  libswresample   5.  1.100 /  5.  1.100
  libpostproc    58.  1.100 / 58.  1.100
Input #0, webp_pipe, from 'zasd.jpeg':
  Duration: N/A, bitrate: N/A
  Stream #0:0: Video: webp, yuv420p(tv, bt470bg/unknown/unknown), 370x555, 25 fps, 25 tbr, 25 tbn
At least one output file must be specified

file: zasd

I cannot download that image file.

I uploaded it again, please try again

zip: zasd.zip img:
zasd

ltdrdata commented 3 months ago

Is it correct that your environment is a local environment and not using network drives or similar?

laisent commented 3 months ago

Is it correct that your environment is a local environment and not using network drives or similar?

No, I was running ComfyUI on a Linux machine. I got the same result on that system, and it only went back to normal after a reboot. So, I was like, 'Guess it's time to blame the hardware!'

System Info: Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.118.1.el7.x86_64 Architecture: x86-64

Python Env Info: Python Env: Python 3.10.6 pillow==10.4.0

ltdrdata commented 3 months ago

Is it correct that your environment is a local environment and not using network drives or similar?

No, I was running ComfyUI on a Linux machine. I got the same result on that system, and it only went back to normal after a reboot. So, I was like, 'Guess it's time to blame the hardware!'

System Info: Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.118.1.el7.x86_64 Architecture: x86-64

Python Env Info: Python Env: Python 3.10.6 pillow==10.4.0

Once this issue starts occurring, does it continue to cause the same issue for all webp files until ComfyUI is shut down?

laisent commented 3 months ago

Is it correct that your environment is a local environment and not using network drives or similar?

No, I was running ComfyUI on a Linux machine. I got the same result on that system, and it only went back to normal after a reboot. So, I was like, 'Guess it's time to blame the hardware!' System Info: Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.118.1.el7.x86_64 Architecture: x86-64 Python Env Info: Python Env: Python 3.10.6 pillow==10.4.0

Once this issue starts occurring, does it continue to cause the same issue for all webp files until ComfyUI is shut down?

Yes, only the webp format has issues; other formats like jpg and png work fine. The problem doesn't resolve until the next restart of ComfyUI.

ltdrdata commented 3 months ago

Is it correct that your environment is a local environment and not using network drives or similar?

No, I was running ComfyUI on a Linux machine. I got the same result on that system, and it only went back to normal after a reboot. So, I was like, 'Guess it's time to blame the hardware!' System Info: Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.118.1.el7.x86_64 Architecture: x86-64 Python Env Info: Python Env: Python 3.10.6 pillow==10.4.0

Once this issue starts occurring, does it continue to cause the same issue for all webp files until ComfyUI is shut down?

Yes, only the webp format has issues; other formats like jpg and png work fine. The problem doesn't resolve until the next restart of ComfyUI.

It seems like a bug in Pillow. How about trying to downgrade it?

lhh-pi commented 2 weeks ago

1728471725087.jpg.zip This image has a similar problem, I have a feeling that the image is not encoded correctly, but I don't know the encoding format of this image

ltdrdata commented 2 weeks ago

1728471725087.jpg.zip This image has a similar problem, I have a feeling that the image is not encoded correctly, but I don't know the encoding format of this image

That's not a jpg file, but a HEIF/HEIC format file. It's not supported in Pillow.