Skip to content

Commit

Permalink
BUG: JPX image without ColorSpace (#2062)
Browse files Browse the repository at this point in the history
Closes #2061
  • Loading branch information
pubpub-zz authored Aug 6, 2023
1 parent 156df85 commit 0987346
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pypdf/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,8 @@ def _get_imagemode(
Image mode not taking into account mask(transparency)
ColorInversion is required (like for some DeviceCMYK)
"""
if isinstance(color_space, NullObject):
return "", False
if isinstance(color_space, str):
pass
elif not isinstance(color_space, list):
Expand Down Expand Up @@ -931,6 +933,9 @@ def _handle_jpx(
extension = ".jp2" # mime_type = "image/x-jp2"
img1 = Image.open(BytesIO(data), formats=("JPEG2000",))
mode, invert_color = _get_imagemode(color_space, colors, mode)
if mode == "":
mode = cast(mode_str_type, img1.mode)
invert_color = mode in ("CMYK",)
if img1.mode == "RGBA" and mode == "RGB":
mode = "RGBA"
# we need to convert to the good mode
Expand Down Expand Up @@ -1028,6 +1033,8 @@ def _handle_jpx(
False,
)
else:
if mode == "":
raise PdfReadError(f"ColorSpace field not found in {x_object_obj}")
img, image_format, extension, invert_color = (
Image.frombytes(mode, size, data),
"PNG",
Expand Down
15 changes: 15 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,18 @@ def test_singleton_device():
name = "pypdf_with_arr_deviceRGB.pdf"
reader = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
reader.pages[0].images[0]


@pytest.mark.enable_socket()
def test_jpx_no_spacecode():
"""From #2061"""
url = "https://github.com/py-pdf/pypdf/files/12253581/tt2.pdf"
name = "jpx_no_spacecode.pdf"
reader = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
im = reader.pages[0].images[0]
# create an object without filter and without colorspace
# just for coverage
del im.indirect_reference.get_object()["/Filter"]
with pytest.raises(PdfReadError) as exc:
reader.pages[0].images[0]
assert exc.value.args[0].startswith("ColorSpace field not found")

0 comments on commit 0987346

Please sign in to comment.