Skip to content

Commit

Permalink
Change .to_image(...)'s approach ...
Browse files Browse the repository at this point in the history
... preferring to composite with a white background instead of removing
the alpha channel. This seems to more reliably produce high-quality
conversions (easier to read, fewer conversion artifacts) than either the
prior or other previous approaches.
  • Loading branch information
jsvine committed Jul 18, 2022
1 parent 35fd429 commit 1cd1f9a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
12 changes: 6 additions & 6 deletions pdfplumber/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ def postprocess(img: WandImage) -> WandImage:

with WandImage(resolution=resolution, filename=filename, file=file) as img_init:
img = postprocess(img_init)
if img.alpha_channel:
img.background_color = WandColor("white")
img.alpha_channel = "remove"
with img.convert("png") as png:
im = PIL.Image.open(BytesIO(png.make_blob()))
return im.convert("RGB")
with WandImage(
width=img.width, height=img.height, background=WandColor("white")
) as bg:
bg.composite(img, 0, 0)
im = PIL.Image.open(BytesIO(bg.make_blob("png")))
return im.convert("RGBA")


class PageImage:
Expand Down
5 changes: 1 addition & 4 deletions tests/test_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,4 @@ def test_outline_chars(self):
def test__repr_png_(self):
png = self.im._repr_png_()
assert isinstance(png, bytes)
assert len(png) in (
71948,
61247,
) # PNG encoder seems to work differently on different setups
assert len(png) == 79774

0 comments on commit 1cd1f9a

Please sign in to comment.