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

Handle truncated chunks at the end of PNG images #7709

Merged
merged 7 commits into from Mar 1, 2024

Conversation

lajiyuan
Copy link
Contributor

@lajiyuan lajiyuan commented Jan 12, 2024

Fix png image plugin load_end func handle truncated file.

When attempting to process PNG image files that are incomplete at the end, the current LOAD_TRUNCATED_IMAGES parameter is unable to handle such cases. It is necessary to add corresponding handling code within the load_end function to ensure proper processing of incomplete images. Below is a link to an example of such an anomalous image:

error_image

Changes proposed in this pull request:

  • Fix PngImagePlugin.py load_end func, add exception handle.

cc @radarhere Can you help me with a code review? thanks a lot.

@radarhere
Copy link
Member

radarhere commented Jan 12, 2024

If you're interested in an immediate solution as an alternative to this PR, I find that you can just ignore the error and the image works fine.

from PIL import Image
im = Image.open('in.png')
try:
    im.load()
except OSError:
    pass
im.save("out.png")

@lajiyuan
Copy link
Contributor Author

I'm amazed by your quick response. Indeed, loading images by ignoring load exceptions can be a way to load and convert images into a normal state. Thank you for your suggestion. Are there any other suggested modifications for this pull request?

@radarhere

Tests/test_file_png.py Outdated Show resolved Hide resolved
@radarhere
Copy link
Member

The test image that you've added is 721kb - this is a bit large.

Here's a test image I've created by modifying hopper_idat_after_image_end.png - it is only 30kb.

load_end_truncated_idat

Tests/test_file_png.py Outdated Show resolved Hide resolved
@radarhere radarhere changed the title Unable to Handle Incomplete PNG Images at the End of File in load_end Function Handle truncated chunks at the end of PNG images Jan 12, 2024
@lajiyuan
Copy link
Contributor Author

@radarhere The comments have all been fixed.

@lajiyuan
Copy link
Contributor Author

Do I need to perform any additional actions for this PR to be merged? @radarhere

Tests/test_file_png.py Outdated Show resolved Hide resolved
@radarhere
Copy link
Member

I've just combined the commits in this PR into one so that your original image isn't part of the commit history.

Tests/test_file_png.py Outdated Show resolved Hide resolved
Tests/test_file_png.py Outdated Show resolved Hide resolved
Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com>
@radarhere radarhere merged commit a20abff into python-pillow:main Mar 1, 2024
56 checks passed
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.

None yet

2 participants