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

Add support to extract gray scale images #1460

Merged
merged 4 commits into from Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Binary file added resources/grayscale.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions tests/test_reader.py
Expand Up @@ -185,6 +185,7 @@ def test_get_outline(src, outline_elements):
marks=pytest.mark.xfail(reason="broken image extraction"),
),
("imagemagick-CCITTFaxDecode.pdf", ["Im0.tiff"]),
("grayscale.pdf", ["X0.png"]),
],
)
def test_get_images(src, expected_images):
Expand Down