Skip to content

"Unhashable type 'ArrayObject'" when extracting images with nested color space definition #2240

@stefan6419846

Description

@stefan6419846

When extracting the images from a PDF file, it seems like a /DeviceN color space contains a nested /ICCBased color space definition, which is not supported at the moment:

# Initial `color_space`.
['/DeviceN', ['/HKS 44 K', '/Magenta', '/Yellow', '/Black'], IndirectObject(7, 0, 140608935739344), IndirectObject(11, 0, 140608935739344), IndirectObject(12, 0, 140608935739344)]
# Retrieved `color_space`.
['/ICCBased', IndirectObject(8, 0, 140608935739344)]

Environment

Which environment were you using when you encountered the problem?

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

$ python -c "import pypdf;print(pypdf._debug_versions)"
pypdf==3.16.2, crypt_provider=('pycryptodome', '3.18.0'), PIL=10.0.0

Code + PDF

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

from pypdf import PdfReader


path = 'out.pdf'
for index, page in enumerate(PdfReader(path).pages):
    print(index, page)
    for key in page.images.keys():
        print(key)
        print(page.images[key].indirect_reference)

The corresponding file is out1.pdf

Traceback

This is the complete traceback I see:

Traceback (most recent call last):
  File "/home/stefan/tmp/run.py", line 12, in <module>
    print(page.images[key].indirect_reference)
  File "/home/stefan/tmp/venv/lib/python3.9/site-packages/pypdf/_page.py", line 2683, in __getitem__
    return self.get_function(index)
  File "/home/stefan/tmp/venv/lib/python3.9/site-packages/pypdf/_page.py", line 555, in _get_image
    return self._get_image(ids, cast(DictionaryObject, xobjs[id[0]]))
  File "/home/stefan/tmp/venv/lib/python3.9/site-packages/pypdf/_page.py", line 544, in _get_image
    imgd = _xobj_to_image(cast(DictionaryObject, xobjs[id]))
  File "/home/stefan/tmp/venv/lib/python3.9/site-packages/pypdf/filters.py", line 761, in _xobj_to_image
    mode, invert_color = _get_imagemode(
  File "/home/stefan/tmp/venv/lib/python3.9/site-packages/pypdf/_xobj_image_helpers.py", line 98, in _get_imagemode
    mode_map.get(color_space)  # type: ignore
TypeError: unhashable type: 'ArrayObject'

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