Skip to content

Cannot open untruncated png image with LOAD_TRUNCATED_IMAGES=True #5491

@rafal-jaworski

Description

@rafal-jaworski

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugAny unexpected behavior, until confirmed feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions