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 argument to raise warnings from factory errors on UnidentifiedImageError #8033

Closed
wants to merge 1 commit into from

Conversation

radarhere
Copy link
Member

Resolves #7993

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.open("bug.png", warn_possible_formats=True)

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: MPEG opening failed. not an MPEG 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'

Comment on lines +122 to +124
with pytest.warns(UserWarning):
with pytest.raises(UnidentifiedImageError):
with Image.open(im, warn_possible_formats=True):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these can all be in the same with statement, separated by commas. Though that might make the line too long.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I do that, and then run black, I get

with pytest.warns(UserWarning), pytest.raises(
    UnidentifiedImageError
), Image.open(im, warn_possible_formats=True):

This does not look clearer to me.

Comment on lines 3347 to +3351
# Leave disabled by default, spams the logs with image
# opening failures that are entirely expected.
# logger.debug("", exc_info=True)
if warn_possible_formats:
warning_messages.append(i + " opening failed. " + str(e))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Leave disabled by default, spams the logs with image
# opening failures that are entirely expected.
# logger.debug("", exc_info=True)
if warn_possible_formats:
warning_messages.append(i + " opening failed. " + str(e))
if warn_possible_formats:
warning_messages.append(i + " opening failed. " + str(e))

The comment is probably no longer relevant with this change.

@radarhere
Copy link
Member Author

Closing due to #7993 (comment).

See the issue for further discussion.

@radarhere radarhere closed this Apr 30, 2024
@radarhere radarhere deleted the possible_formats branch April 30, 2024 08:36
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)
3 participants