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

Failure to identify PNG image #8669

Closed
Kelketek opened this issue Jan 7, 2025 · 7 comments
Closed

Failure to identify PNG image #8669

Kelketek opened this issue Jan 7, 2025 · 7 comments

Comments

@Kelketek
Copy link

Kelketek commented Jan 7, 2025

What did you do?

Attempted to read a PNG image file usable by most applications

What did you expect to happen?

Pillow reads and processed the PNG.

What actually happened?

PIL.UnidentifiedImageError: cannot identify image file <_io.BufferedReader name='example.png'>

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 22.04.5 LTS
  • Python: 3.11.1
  • Pillow: 11.0.01
--------------------------------------------------------------------
Pillow 11.0.0
Python 3.11.1 (main, Jan 31 2023, 09:47:18) [GCC 11.3.0]
--------------------------------------------------------------------
Python executable is /home/fox/.pyenv/versions/artconomy/bin/python3
Environment Python files loaded from /home/fox/.pyenv/versions/artconomy
System Python files loaded from /home/fox/.pyenv/versions/3.11.1
--------------------------------------------------------------------
Python Pillow modules loaded from /home/fox/.pyenv/versions/artconomy/lib/python3.11/site-packages/PIL
Binary Pillow modules loaded from /home/fox/.pyenv/versions/artconomy/lib/python3.11/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 11.0.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.13.2
--- LITTLECMS2 support ok, loaded 2.16
--- WEBP support ok, loaded 1.4.0
--- JPEG support ok, compiled for libjpeg-turbo 3.0.4
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.2
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.11
--- LIBTIFF support ok, loaded 4.6.0
--- RAQM (Bidirectional Text) support ok, loaded 0.10.1, fribidi 1.0.8, harfbuzz 10.0.1
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
from PIL import Image
from io import BytesIO

with open('example.png', 'rb') as image_file:
    Image.open(image_file)

File here:

example

@radarhere
Copy link
Member

If I run pngcheck example.png, I get

example.png  CRC error in chunk pHYs (computed d2dd7efc, expected 679fd252)
ERROR: example.png

So there is a problem with your image.

If you would like to ignore the problem, you can do so with LOAD_TRUNCATED_IMAGES.

from PIL import Image, ImageFile
from io import BytesIO

ImageFile.LOAD_TRUNCATED_IMAGES = True

with open('example.png', 'rb') as image_file:
    Image.open(image_file)

@radarhere radarhere changed the title Failure to identify image Failure to identify PNG image Jan 7, 2025
@Kelketek
Copy link
Author

Kelketek commented Jan 7, 2025

Thanks, @radarhere . The site I'm working on here allows general uploads by the public-- is there any security implication to allowing LOAD_TRUNCATED_IMAGES or is this only a check on internal consistency?

Are there any other concerns I should be aware of?

@radarhere
Copy link
Member

#6843 (comment)

a truncated image is an image that is truncated, e,g, #3023. For PNGs, the definition of this is broadened to include images that have missing chunk data or invalid chunk checksums.

@Kelketek
Copy link
Author

Kelketek commented Jan 9, 2025

Got it. Thanks, @radarhere . Just to make sure, this is still a valid issue in your view, right? Any error with actually processing the image should happen upon loading, but identification shouldn't fail here?

@radarhere
Copy link
Member

That idea has been suggested before in #1687

My conclusion was

I don't think our formats allow for this.

  • FpxImagePlugin initially _accepts an image, but may later decide it's not FPX after all. So if an error occurs before that second line, it shouldn't be marked as identified. I don't see how you could communicate that distinction purely by catching errors without further obscuring the original cause.
  • ImtImagePlugin does not have a good ability to distinguish between a malformed file and a file of another type.

It could have been a good idea if we were starting Pillow from scratch, but for a library concerned with backwards compatibility, I don't think it can be achieved without rewrites that seem excessive.

@Kelketek
Copy link
Author

@radarhere In that case, should this issue be closed or is there anything to be done here?

@radarhere
Copy link
Member

I don't think there is anything further, no.

We've even documented this scenario to try and help further - https://pillow.readthedocs.io/en/stable/PIL.html#PIL.UnidentifiedImageError
Screenshot 2025-01-10 at 3 24 22 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants