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

Cannot open untruncated png image with LOAD_TRUNCATED_IMAGES=True #5491

Closed
rafal-jaworski opened this issue May 13, 2021 · 2 comments · Fixed by #5493
Closed

Cannot open untruncated png image with LOAD_TRUNCATED_IMAGES=True #5491

rafal-jaworski opened this issue May 13, 2021 · 2 comments · Fixed by #5493
Labels
Bug Any unexpected behavior, until confirmed feature.

Comments

@rafal-jaworski
Copy link

What did you do?

Open an untruncated png image with LOAD_TRUNCATED_IMAGES==True.

What did you expect to happen?

The image is opened.

What actually happened?

The image is not opened. (But image converted with gimp opens)

Loaded PIL.ImageFile.LOAD_TRUNCATED_IMAGES=False
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/.virtualenvs/venv/lib64/python3.8/site-packages/PIL/PngImagePlugin.py in load_end(self)
    939             try:
--> 940                 self.png.call(cid, pos, length)
    941             except UnicodeDecodeError:

~/.virtualenvs/venv/lib64/python3.8/site-packages/PIL/PngImagePlugin.py in call(self, cid, pos, length)
    186         logger.debug("STREAM %r %s %s", cid, pos, length)
--> 187         return getattr(self, "chunk_" + cid.decode("ascii"))(pos, length)
    188 

AttributeError: 'PngStream' object has no attribute 'chunk_I'

During handling of the above exception, another exception occurred:

OSError                                   Traceback (most recent call last)
<ipython-input-14-39acebf157ec> in <module>
      9 
     10 img = PIL.Image.open(image_path)
---> 11 img.load()
     12 print(f"Loaded {PIL.ImageFile.LOAD_TRUNCATED_IMAGES=}")

~/.virtualenvs/venv/lib64/python3.8/site-packages/PIL/ImageFile.py in load(self)
    264         self.readonly = readonly
    265 
--> 266         self.load_end()
    267 
    268         if self._exclusive_fp and self._close_exclusive_fp_after_loading:

~/.virtualenvs/venv/lib64/python3.8/site-packages/PIL/PngImagePlugin.py in load_end(self)
    947             except AttributeError:
    948                 logger.debug("%r %s %s (unknown)", cid, pos, length)
--> 949                 s = ImageFile._safe_read(self.fp, length)
    950                 if cid[1:2].islower():
    951                     self.private_chunks.append((cid, s, True))

~/.virtualenvs/venv/lib64/python3.8/site-packages/PIL/ImageFile.py in _safe_read(fp, size)
    566         size -= len(block)
    567     if sum(len(d) for d in data) < size:
--> 568         raise OSError("Truncated File Read")
    569     return b"".join(data)
    570 

OSError: Truncated File Read

What are your OS, Python and Pillow versions?

import PIL
image_path = 'error_image.png'

img = PIL.Image.open(image_path)
img.load()
print(f"Loaded {PIL.ImageFile.LOAD_TRUNCATED_IMAGES=}")

PIL.ImageFile.LOAD_TRUNCATED_IMAGES = True

img = PIL.Image.open(image_path)
img.load()
print(f"Loaded {PIL.ImageFile.LOAD_TRUNCATED_IMAGES=}")

error_image
gimp

@rafal-jaworski rafal-jaworski changed the title Open untruncated png image with LOAD_TRUNCATED_IMAGES Cannot open untruncated png image with LOAD_TRUNCATED_IMAGES=True May 13, 2021
@wiredfool
Copy link
Member

The problem is that the png apparently has a "I" chunk, which is invalid, as chunk IDs are 4 characters.

@radarhere
Copy link
Member

While that is what Pillow is reporting, if I run pngcheck -v on the first image, the problematic chunk is correctly identified as IEND.

The problem is that the IDAT chunk before it has padding, and Pillow stops progressing through the chunk before it is done. i've created #5493 to resolve this.

@radarhere radarhere added the Bug Any unexpected behavior, until confirmed feature. label May 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Any unexpected behavior, until confirmed feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants