Skip to content

Commit

Permalink
Add Indexed color space support
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Jul 9, 2022
1 parent aec3a99 commit 408bff5
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions PyPDF2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,21 @@ def _xobj_to_image(x_object_obj: Dict[str, Any]) -> Tuple[Optional[str], bytes]:
if SA.FILTER in x_object_obj:
if x_object_obj[SA.FILTER] == FT.FLATE_DECODE:
extension = ".png"
color_space = None
if "/ColorSpace" in x_object_obj:
color_space = x_object_obj["/ColorSpace"].get_object()
if (
isinstance(color_space, ArrayObject)
and color_space[0] == "/Indexed"
):
color_space, base, hival, lookup = (
value.get_object() for value in color_space
)

img = Image.frombytes(mode, size, data)
if color_space == "/Indexed":
img.putpalette(lookup.get_data())
img = img.convert("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

0 comments on commit 408bff5

Please sign in to comment.