Skip to content

Image extraction raises "ValueError: illegal image mode" #2030

@stefan6419846

Description

@stefan6419846

I am trying to extract the images from a PDF file, which fails with "illegal image mode"

Environment

Which environment were you using when you encountered the problem?

$ python -m platform
Linux-5.14.21-150400.24.69-default-x86_64-with-glibc2.31

$ python -c "import pypdf;print(pypdf.__version__)"
3.13.0

Code + PDF

This is a minimal, complete example that shows the issue:

from pypdf import PdfReader

reader = PdfReader('redacted.pdf')
for page in reader.pages:
    for image in reader.images:
        print(image)

I have generated a mostly empty version of the document reproducing this issue: redacted.pdf

Traceback

This is the complete traceback I see:

Traceback (most recent call last):
  File "/home/stefan/temp/analyze.py", line 17, in <module>
    for image in page.images:
  File "/home/stefan/temp/pypdf/pypdf/_page.py", line 2604, in __iter__
    yield self[i]
  File "/home/stefan/temp/pypdf/pypdf/_page.py", line 2600, in __getitem__
    return self.get_function(lst[index])
  File "/home/stefan/temp/pypdf/pypdf/_page.py", line 522, in _get_image
    imgd = _xobj_to_image(cast(DictionaryObject, xobjs[id]))
  File "/home/stefan/temp/pypdf/pypdf/filters.py", line 892, in _xobj_to_image
    img, image_format, extension = _handle_flate(
  File "/home/stefan/temp/pypdf/pypdf/filters.py", line 804, in _handle_flate
    img.putpalette(lookup.get_data())
  File "/home/stefan/temp/venv/lib64/python3.9/site-packages/PIL/Image.py", line 1914, in putpalette
    raise ValueError("illegal image mode")
ValueError: illegal image mode

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions