Skip to content

Commit

Permalink
More tests with images
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Apr 15, 2022
1 parent d0e4318 commit ce82f40
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
10 changes: 10 additions & 0 deletions PyPDF2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,16 @@ def _xobj_to_image(x_object_obj):
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format="PNG")
data = img_byte_arr.getvalue()
elif x_object_obj["/Filter"] == ["/LZWDecode"]:
from PyPDF2.utils import b_
extension = ".png"
img = Image.frombytes(mode, size, b_(data))
if "/SMask" in x_object_obj: # add alpha channel
alpha = Image.frombytes("L", size, x_object_obj["/SMask"].getData())
img.putalpha(alpha)
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format="PNG")
data = img_byte_arr.getvalue()
elif x_object_obj["/Filter"] == "/DCTDecode":
extension = ".jpg"
elif x_object_obj["/Filter"] == "/JPXDecode":
Expand Down
Binary file added Resources/imagemagick-images.pdf
Binary file not shown.
Binary file added Resources/imagemagick-lzw.pdf
Binary file not shown.
4 changes: 4 additions & 0 deletions Tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
os.path.join(RESOURCE_ROOT, "libreoffice-writer-password.pdf"),
"openpassword",
),
(os.path.join(RESOURCE_ROOT, "imagemagick-images.pdf"), None),
(os.path.join(RESOURCE_ROOT, "imagemagick-lzw.pdf"), None),
],
ids=[
"crazyones",
"attachment",
"side-by-side-subfig",
"libreoffice-writer-password",
"imagemagick-images",
"imagemagick-lzw",
],
)
def test_page_operations(pdf_path, password):
Expand Down
7 changes: 5 additions & 2 deletions Tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def test_get_outlines(src, outline_elements):
(os.path.join(RESOURCE_ROOT, "pdflatex-outline.pdf"), 0),
(os.path.join(RESOURCE_ROOT, "crazyones.pdf"), 0),
(os.path.join(RESOURCE_ROOT, "git.pdf"), 1),
(os.path.join(RESOURCE_ROOT, "imagemagick-lzw.pdf"), 1),
],
)
def test_get_images(src, nb_images):
Expand All @@ -135,11 +136,13 @@ def test_get_images(src, nb_images):
with open(filename, "wb") as img:
img.write(byte_stream)
images_extracted.append(filename)
else:
pass # No image found

assert len(images_extracted) == nb_images

# Cleanup
for filepath in images_extracted:
os.remove(filepath)


@pytest.mark.parametrize(
"strict,with_prev_0,should_fail",
Expand Down

0 comments on commit ce82f40

Please sign in to comment.