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: Process lookup decoded as TextStringObjects #2008

Merged
merged 2 commits into from
Jul 25, 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
6 changes: 4 additions & 2 deletions pypdf/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,9 +764,11 @@ def bits2byte(data: bytes, size: Tuple[int, int], bits: int) -> bytes:
data = bits2byte(data, size, 4)
img = Image.frombytes(mode, size, data)
if color_space == "/Indexed":
from .generic import ByteStringObject
from .generic import TextStringObject

if isinstance(lookup, ByteStringObject):
if isinstance(lookup, TextStringObject):
lookup = lookup.original_bytes
if isinstance(lookup, bytes):
try:
nb, conv, mode = { # type: ignore
"1": (0, "", ""),
Expand Down
34 changes: 34 additions & 0 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,40 @@ def test_calrgb():
reader.pages[0].images[0]


@pytest.mark.enable_socket()
def test_index_lookup():
"""The lookup is provided as an str and bytes"""
url = "https://github.com/py-pdf/pypdf/files/12090523/2023.USDC_Circle.Examination.Report.May.2023.pdf"
name = "2023USDC.pdf"
reader = PdfReader(BytesIO(get_pdf_from_url(url, name=name)))
# TextStringObject Lookup
url_png = "https://github.com/py-pdf/pypdf/files/12144094/im1.png.txt"
name_png = "iss1982_im1.png"
refimg = Image.open(
BytesIO(get_pdf_from_url(url_png, name=name_png))
) # not a pdf but it works
data = reader.pages[0].images[-1]
assert data.image.mode == "RGB"
diff = ImageChops.difference(data.image, refimg)
d = sqrt(sum([(a * a + b * b + c * c) for a, b, c in diff.getdata()])) / (
diff.size[0] * diff.size[1]
)
assert d < 0.001
# ByteStringObject Lookup
url_png = "https://github.com/py-pdf/pypdf/files/12144093/im2.png.txt"
name_png = "iss1982_im2.png"
refimg = Image.open(
BytesIO(get_pdf_from_url(url_png, name=name_png))
) # not a pdf but it works
data = reader.pages[-1].images[-1]
assert data.image.mode == "RGB"
diff = ImageChops.difference(data.image, refimg)
d = sqrt(sum([(a * a + b * b + c * c) for a, b, c in diff.getdata()])) / (
diff.size[0] * diff.size[1]
)
assert d < 0.001


@pytest.mark.enable_socket()
def test_2bits_image():
"""From #1954, test with 2bits image. TODO: 4bits also"""
Expand Down