Skip to content

Commit

Permalink
Merge pull request #496 from chinapandaman/PPF-495
Browse files Browse the repository at this point in the history
PPF-495: set alpha to white when drawing image
  • Loading branch information
chinapandaman authored Feb 14, 2024
2 parents ee2e15d + 5363f7c commit c777709
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion PyPDFForm/core/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def any_image_to_jpg(image_stream: bytes) -> bytes:
buff.close()
return image_stream

rgb_image = image.convert("RGB")
rgb_image = Image.new("RGB", image.size, (255, 255, 255))
rgb_image.paste(image, mask=image.split()[3])

with BytesIO() as _file:
rgb_image.save(_file, format="JPEG")
_file.seek(0)
Expand Down
Binary file added image_samples/sample_transparent_png.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pdf_samples/sample_pdf_with_png_image.pdf
Binary file not shown.
Binary file added pdf_samples/sample_pdf_with_transparent_png.pdf
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,29 @@ def test_draw_png_image_on_one_page(
assert obj.stream == expected


def test_draw_transparent_png_image_on_one_page(
template_stream, image_samples, pdf_samples, request
):
expected_path = os.path.join(pdf_samples, "sample_pdf_with_transparent_png.pdf")
with open(expected_path, "rb+") as f:
obj = PdfWrapper(template_stream).draw_image(
os.path.join(image_samples, "sample_transparent_png.png"),
1,
100,
100,
400,
225,
)

expected = f.read()

if os.name == "nt":
request.config.results["expected_path"] = expected_path
request.config.results["stream"] = obj.read()
assert len(obj.stream) == len(expected)
assert obj.stream == expected


def test_addition_operator_3_times(template_stream, pdf_samples, data_dict, request):
expected_path = os.path.join(pdf_samples, "sample_added_3_copies.pdf")
with open(expected_path, "rb+") as f:
Expand Down

0 comments on commit c777709

Please sign in to comment.