Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

Handle truncated chunks at the end of PNG images

Open lajiyuan opened this issue 1 year ago • 6 comments

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.

lajiyuan avatar Jan 12 '24 07:01 lajiyuan

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")

radarhere avatar Jan 12 '24 08:01 radarhere

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

lajiyuan avatar Jan 12 '24 08:01 lajiyuan

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

radarhere avatar Jan 12 '24 10:01 radarhere

@radarhere The comments have all been fixed.

lajiyuan avatar Jan 12 '24 10:01 lajiyuan

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

lajiyuan avatar Jan 15 '24 02:01 lajiyuan

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

radarhere avatar Jan 16 '24 07:01 radarhere