Skip to content

Commit

Permalink
BUG : JPX image wit no ColorSpace
Browse files Browse the repository at this point in the history
closes #2061
  • Loading branch information
pubpub-zz committed Aug 3, 2023
1 parent c04a6bb commit 37b6b2e
Show file tree
Hide file tree
Showing 2 changed files with 16 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 = 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
9 changes: 9 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,12 @@ 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)))
reader.pages[0].images[0]

0 comments on commit 37b6b2e

Please sign in to comment.