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

ROB: Cope with some issues in pillow #2595

Merged
merged 8 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 10 additions & 5 deletions pypdf/_xobj_image_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@
color_components = cast(int, icc_profile["/N"])
color_space = icc_profile.get("/Alternate", "")
elif color_space[0] == "/Indexed":
color_space = color_space[1]
if isinstance(color_space, IndirectObject):
color_space = color_space.get_object()
color_space = color_space[1].get_object()
mode2, invert_color = _get_imagemode(
color_space, color_components, prev_mode, depth + 1
)
Expand Down Expand Up @@ -292,10 +290,17 @@
mode = "RGBA"
# we need to convert to the good mode
try:
if img1.mode != mode:
if (img1.mode == mode) or (img1.mode in ("L", "P") and mode in ("L", "P")):
pubpub-zz marked this conversation as resolved.
Show resolved Hide resolved
img = img1
elif (
img1.mode == "RGBA"
and mode == "CMYK"
or img1.mode == "CMYK"
and mode == "RGBA"
):
pubpub-zz marked this conversation as resolved.
Show resolved Hide resolved
img = Image.frombytes(mode, img1.size, img1.tobytes())
else:
img = img1
img = img1.convert(mode)

Check warning on line 303 in pypdf/_xobj_image_helpers.py

View check run for this annotation

Codecov / codecov/patch

pypdf/_xobj_image_helpers.py#L303

Added line #L303 was not covered by tests
except OSError:
img = Image.frombytes(mode, img1.size, img1.tobytes())
# for CMYK conversion :
Expand Down
9 changes: 6 additions & 3 deletions pypdf/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,10 +894,13 @@
img_byte_arr = BytesIO()
try:
img.save(img_byte_arr, format=image_format)
except OSError: # pragma: no cover
# odd error
except OSError:

Check warning on line 897 in pypdf/filters.py

View check run for this annotation

Codecov / codecov/patch

pypdf/filters.py#L897

Added line #L897 was not covered by tests
# in case of we convert to RGBA and then to PNG
img1 = img.convert("RGBA")
image_format = "PNG"
extension = ".png"

Check warning on line 901 in pypdf/filters.py

View check run for this annotation

Codecov / codecov/patch

pypdf/filters.py#L899-L901

Added lines #L899 - L901 were not covered by tests
img_byte_arr = BytesIO()
img.save(img_byte_arr, format=image_format)
img1.save(img_byte_arr, format=image_format)

Check warning on line 903 in pypdf/filters.py

View check run for this annotation

Codecov / codecov/patch

pypdf/filters.py#L903

Added line #L903 was not covered by tests
data = img_byte_arr.getvalue()

try: # temporary try/except until other fixes of images
Expand Down
11 changes: 11 additions & 0 deletions tests/test_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,14 @@ def test_data_with_lf():
name = "iss2343b0.png"
img = Image.open(BytesIO(get_data_from_url(url, name=name)))
assert image_similarity(reader.pages[8].images[9].image, img) == 1.0


@pytest.mark.enable_socket()
def test_oserror():
"""Cf #2265"""
url = "https:/py-pdf/pypdf/files/13127130/Binance.discovery.responses.2.gov.uscourts.dcd.256060.140.1.pdf"
name = "iss2265.pdf"
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
reader.pages[2].images[1]
# due to errors in translation in pillow we may not be have to get
pubpub-zz marked this conversation as resolved.
Show resolved Hide resolved
# the correct image therefore we cannot use image_similarity
pubpub-zz marked this conversation as resolved.
Show resolved Hide resolved
Loading