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

Added Image.WARN_POSSIBLE_FORMATS #8063

Merged
merged 5 commits into from
Jun 28, 2024

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented May 16, 2024

Resolves #7993. Alternative to #8033

If a user tries to open the image from that issue

from PIL import Image
Image.open("bug.png")

they will see

Traceback (most recent call last):
  File "demo.py", line 2, in <module>
    Image.open("bug.png")
  File "PIL/Image.py", line 3380, in open
    raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file 'bug.png'

If the user would like to investigate further, this PR adds "warn_possible_formats". It will show any errors raised during the checking of the formats as warnings.

from PIL import Image
Image.WARN_POSSIBLE_FORMATS = True
Image.open("bug.png")

giving

PIL/Image.py:3378: UserWarning: PNG opening failed. broken PNG file (bad header checksum in b'pHYs')
  warnings.warn(message)
PIL/Image.py:3378: UserWarning: IM opening failed. Syntax error in IM header: �PNG
  warnings.warn(message)
PIL/Image.py:3378: UserWarning: IMT opening failed. not identified by this driver
  warnings.warn(message)
PIL/Image.py:3378: UserWarning: IPTC opening failed. invalid IPTC/NAA file
  warnings.warn(message)
PIL/Image.py:3378: UserWarning: PCD opening failed. not a PCD file
  warnings.warn(message)
PIL/Image.py:3378: UserWarning: SPIDER opening failed. not a valid Spider file
  warnings.warn(message)
PIL/Image.py:3378: UserWarning: TGA opening failed. not a TGA file
  warnings.warn(message)
Traceback (most recent call last):
  File "demo.py", line 3, in <module>
    Image.open("bug.png", warn_possible_formats=True)
  File "PIL/Image.py", line 3380, in open
    raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file 'bug.png'

src/PIL/Image.py Outdated Show resolved Hide resolved
Tests/test_image.py Outdated Show resolved Hide resolved
@hugovk hugovk merged commit 3fdaecb into python-pillow:main Jun 28, 2024
56 checks passed
@radarhere radarhere deleted the possible_formats branch June 28, 2024 07:32
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

Successfully merging this pull request may close these issues.

cannot identify image file (PNG file from scanner)
4 participants