Closed robonrrd closed 2 months ago
Hi. You are hitting a limit when opening images.
https://pillow.readthedocs.io/en/stable/reference/plugins.html#PIL.PngImagePlugin.MAX_TEXT_CHUNK
PIL.PngImagePlugin.MAX_TEXT_CHUNK = 1048576 Maximum decompressed size for a iTXt or zTXt chunk. Eliminates decompression bombs where compressed chunks can expand 1000x. See Text in PNG File Format.
You can increase this in your script like so.
from PIL import Image, PngImagePlugin
PngImagePlugin.MAX_TEXT_CHUNK = 2 * 1024 * 1024
Image.open('001041.png')
That fixes my proximate issue (I can now open this image), thank you.
However, I would still claim this is a bug. It was a PNG generated by Pillow and the naive user (me!) would expect Pillow to also be able to open it. Further, OpenCV (and GIMP and qiv, etc.) can open it with no adjustment of internal constants.
Are you able to provide a self-contained example to show how you generated it?
I used some code that uses blenderproc to render 3D objects on top of background images. I used the OpenImages V7 dataset as a source of background images.
The problematic part of the image is the ICC profile, which is just over 1mb. We are raising an error to try and prevent decompression bombs, where compressed data becomes excessively large when expanded.
Without a self-contained example, it's hard to go into further detail, but it would appear that something external to Pillow is providing a large ICC profile. I think there is an expectation that Pillow saves exactly the data that it is given. I considered splitting it into multiple iCCP chunks, but http://www.libpng.org/pub/png/spec/1.2/PNG-Chunks.html#C.iCCP doesn't say anything about concatenating.
We could raise an error when saving a large ICC profile, but I don't think you would find that helpful.
If there was a self-contained example, we could try and figure out where the ICC profile comes from, but otherwise, I don't think there is anything for us to do here.
@robonrrd do you have any further thoughts?
I still feel that this is a bug, or at least a bad preset that causes problems. My reasoning is that Pillow is the only image manipulation library I use which has a problem with this image and, further, the error message is not instructive and the fix is non-intuitive. I would never have been able to diagnose this without your help.
Perhaps the easiest solution is to kick the can down the road and increase the default maximum decompressed size? If you think changing the default max size is bad, perhaps change the error message to suggest that the user increase the size.
I've created #8391. That will change the error message to "Decompressed data too large for PngImagePlugin.MAX_TEXT_CHUNK".
If a user sees that, and searches our docs for "PngImagePlugin.MAX_TEXT_CHUNK", they will find https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#png-opening
Individual compressed chunks are limited to a decompressed size of PngImagePlugin.MAX_TEXT_CHUNK, by default 1MB, to prevent decompression bombs.
What did you do?
I attempted to open a PNG, created with Pillow.
What did you expect to happen?
For the image to be successfully opened.
What actually happened?
An exception was thrown:
ValueError: Decompressed Data Too Large
What are your OS, Python and Pillow versions?