Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: JPX image without ColorSpace #2062

Merged
merged 3 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")