Skip to content

Commit

Permalink
Add support to extract gray scale images (#1460)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeywang4 committed Dec 9, 2022
1 parent 7f586ae commit 22214e8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
4 changes: 3 additions & 1 deletion PyPDF2/filters.py
Expand Up @@ -602,10 +602,12 @@ def _xobj_to_image(x_object_obj: Dict[str, Any]) -> Tuple[Optional[str], bytes]:
from .generic import ByteStringObject

if isinstance(lookup, ByteStringObject):
if base == ColorSpaces.DEVICE_GRAY and len(lookup) == hival + 1:
lookup = b"".join([lookup[i:i + 1] * 3 for i in range(len(lookup))])
img.putpalette(lookup)
else:
img.putpalette(lookup.get_data())
img = img.convert("RGB")
img = img.convert("L" if base == ColorSpaces.DEVICE_GRAY else "RGB")
if G.S_MASK in x_object_obj: # add alpha channel
alpha = Image.frombytes("L", size, x_object_obj[G.S_MASK].get_data())
img.putalpha(alpha)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_reader.py
Expand Up @@ -192,6 +192,7 @@ def test_get_outline(src, outline_elements):
marks=pytest.mark.xfail(reason="broken image extraction"),
),
("imagemagick-CCITTFaxDecode.pdf", ["Im0.tiff"]),
(SAMPLE_ROOT / "019-grayscale-image/grayscale-image.pdf", ["X0.png"]),
],
)
def test_get_images(src, expected_images):
Expand All @@ -211,7 +212,7 @@ def test_get_images(src, expected_images):
for image, expected_image in zip(images_extracted, expected_images):
assert image.name == expected_image
try:
fn = f"test-out-{src}-{image.name}"
fn = f"{src}-test-out-{image.name}"
with open(fn, "wb") as fp:
fp.write(image.data)
assert (
Expand Down

0 comments on commit 22214e8

Please sign in to comment.