Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradio seems to break Pillow / PIL.Image format support #2843

Closed
1 task done
jokkebk opened this issue Dec 18, 2022 · 6 comments · Fixed by #2846
Closed
1 task done

Gradio seems to break Pillow / PIL.Image format support #2843

jokkebk opened this issue Dec 18, 2022 · 6 comments · Fixed by #2846
Assignees
Labels
bug Something isn't working

Comments

@jokkebk
Copy link

jokkebk commented Dec 18, 2022

Describe the bug

Encountered this on latest Automatic1111 Stable Diffusion Webui, suddenly PIL on my Win10 machine had lost all but PNG support:

  1. Importing PIL.Image and printing registered_extensions gives me about 20 file formats
  2. BUT if I import gradio first and then PIL.Image, I only get {'.png': 'PNG', '.apng': 'PNG'}

I am not well versed enough with Gradio to understand what gets broken, my hunch would be Gradio tinkering with Pillow (as it is one requirements) upon initialization, or Gradio introducing some global variable/function that "pollutes" PIL.Image initialization.

Tried with WSL2 on Ubuntu (Gradio 3.14) and same happens, so it is not "some Windows issue" 😇

Workaround is to import PIL.Image before gradio, but this should not happen.

Is there an existing issue for this?

  • I have searched the existing issues

Reproduction

Normal PIL Image support:

from PIL import Image
print(Image.registered_extensions())

Outputs {'.blp': 'BLP', '.bmp': 'BMP', '.dib': 'DIB', '.bufr': 'BUFR', '.cur': 'CUR', '.pcx': 'PCX', '.dcx': 'DCX', '.dds': 'DDS', '.ps': 'EPS', '.eps': 'EPS', '.fit': 'FITS', '.fits': 'FITS', '.fli': 'FLI', '.flc': 'FLI', '.ftc': 'FTEX', '.ftu': 'FTEX', '.gbr': 'GBR', '.gif': 'GIF', '.grib': 'GRIB', '.h5': 'HDF5', '.hdf': 'HDF5', '.png': 'PNG', '.apng': 'PNG', '.jp2': 'JPEG2000', '.j2k': 'JPEG2000', '.jpc': 'JPEG2000', '.jpf': 'JPEG2000', '.jpx': 'JPEG2000', '.j2c': 'JPEG2000', '.icns': 'ICNS', '.ico': 'ICO', '.im': 'IM', '.iim': 'IPTC', '.tif': 'TIFF', '.tiff': 'TIFF', '.jfif': 'JPEG', '.jpe': 'JPEG', '.jpg': 'JPEG', '.jpeg': 'JPEG', '.mpg': 'MPEG', '.mpeg': 'MPEG', '.mpo': 'MPO', '.msp': 'MSP', '.palm': 'PALM', '.pcd': 'PCD', '.pdf': 'PDF', '.pxr': 'PIXAR', '.pbm': 'PPM', '.pgm': 'PPM', '.ppm': 'PPM', '.pnm': 'PPM', '.psd': 'PSD', '.bw': 'SGI', '.rgb': 'SGI', '.rgba': 'SGI', '.sgi': 'SGI', '.ras': 'SUN', '.tga': 'TGA', '.icb': 'TGA', '.vda': 'TGA', '.vst': 'TGA', '.webp': 'WEBP', '.wmf': 'WMF', '.emf': 'WMF', '.xbm': 'XBM', '.xpm': 'XPM'}

With Gradio imported first:

import gradio
from PIL import Image
print(Image.registered_extensions())

Outputs only {'.png': 'PNG', '.apng': 'PNG'}

Screenshot

$ python3
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

from PIL import Image
Image.registered_extensions()
{'.blp': 'BLP', '.bmp': 'BMP', '.dib': 'DIB', '.bufr': 'BUFR', '.cur': 'CUR', '.pcx': 'PCX', '.dcx': 'DCX', '.dds': 'DDS', '.ps': 'EPS', '.eps': 'EPS', '.fit': 'FITS', '.fits': 'FITS', '.fli': 'FLI', '.flc': 'FLI', '.ftc': 'FTEX', '.ftu': 'FTEX', '.gbr': 'GBR', '.gif': 'GIF', '.grib': 'GRIB', '.h5': 'HDF5', '.hdf': 'HDF5', '.png': 'PNG', '.apng': 'PNG', '.jp2': 'JPEG2000', '.j2k': 'JPEG2000', '.jpc': 'JPEG2000', '.jpf': 'JPEG2000', '.jpx': 'JPEG2000', '.j2c': 'JPEG2000', '.icns': 'ICNS', '.ico': 'ICO', '.im': 'IM', '.iim': 'IPTC', '.tif': 'TIFF', '.tiff': 'TIFF', '.jfif': 'JPEG', '.jpe': 'JPEG', '.jpg': 'JPEG', '.jpeg': 'JPEG', '.mpg': 'MPEG', '.mpeg': 'MPEG', '.mpo': 'MPO', '.msp': 'MSP', '.palm': 'PALM', '.pcd': 'PCD', '.pdf': 'PDF', '.pxr': 'PIXAR', '.pbm': 'PPM', '.pgm': 'PPM', '.ppm': 'PPM', '.pnm': 'PPM', '.psd': 'PSD', '.bw': 'SGI', '.rgb': 'SGI', '.rgba': 'SGI', '.sgi': 'SGI', '.ras': 'SUN', '.tga': 'TGA', '.icb': 'TGA', '.vda': 'TGA', '.vst': 'TGA', '.webp': 'WEBP', '.wmf': 'WMF', '.emf': 'WMF', '.xbm': 'XBM', '.xpm': 'XPM'}

$ python3
Python 3.8.10 (default, Jun 22 2022, 20:18:18)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

import gradio
from PIL import Image
Image.registered_extensions()
{'.png': 'PNG', '.apng': 'PNG'}

Logs

See the "screenshot" above.

System Info

Gradio 3.14.0 (and also tested on 3.9). Windows 10. No browser needed to replicate.
Gradio 3.14.0 on Ubuntu (under WSL2) does the same.

Severity

serious, but I can work around it

@jokkebk jokkebk added the bug Something isn't working label Dec 18, 2022
@aliencaocao
Copy link
Contributor

@aliencaocao
Copy link
Contributor

@jokkebk can you see my issue report python-pillow/Pillow#6809 and if you can reproduce what I discovered? It would help if you also managed to reproduce and comment under the issue so they can notice it quickly.

@jokkebk
Copy link
Author

jokkebk commented Dec 18, 2022

Wow, awesome detective work @aliencaocao ! Tried your example in pillow issue report and can replicate, I commented there as well.

So it seems like this is an issue with Pillow's PngImagePlugin (or some interplay of its internal components), just that it is triggered by Gradio that uses both.

@abidlabs
Copy link
Member

Very interesting, thanks for pointing out the issue @jokkebk as well as the underlying problem @aliencaocao. It sounds like the issue needs to be addressed in PIL, but we'll look to see if there's something we can do in gradio too as a patch.

@abidlabs abidlabs self-assigned this Dec 18, 2022
@aliencaocao
Copy link
Contributor

aliencaocao commented Dec 19, 2022

@abidlabs following PIL author's suggestion, gradio should add the following code:

from PIL import Image
Image.init()

python-pillow/Pillow#6809 (comment)

They have also made a PR that is pending to be merged, but the code above should be the proper patch before the PR is merged and they release a new version (which would be in Jan 2023)

I have made a PR: #2846

@miguelwon
Copy link

miguelwon commented Jan 3, 2024

I'm having this issue, probably related. The format parameter returns None. Simple example from the documentation:

import gradio as gr
import os


def image_mod(image):
    print("Format:", image.format)
    return image.rotate(45)


demo = gr.Interface(
    image_mod,
    gr.Image(type="pil"),
    "image",
    flagging_options=["blurry", "incorrect", "other"],
    examples=[
        os.path.join(os.path.dirname(__file__), "images/cheetah1.png"),
        os.path.join(os.path.dirname(__file__), "images/lion.png"),
    ],
)

if __name__ == "__main__":
    demo.launch()

prints:

Format: None

I'm using:

>>> import gradio,PIL
>>> gradio.__version__
'4.12.0'
>>> PIL.__version__
'10.0.1'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants