Skip to content

I want to add transparency image to PDF,But the effect is not very good, the image shows a gray area #1005

@jmu201621143028

Description

@jmu201621143028

This my code

import fitz
from PIL import Image
import io
# https://pymupdf.readthedocs.io/en/latest/page.html#Page.insert_image

input_file = "test.pdf"
output_file = "output.pdf"
water_img = "tar.png"


# conversion Transparent
img = Image.open(water_img)
img.putalpha(128)
# conversion stream
imgbuf = io.BytesIO()
img.save(imgbuf, 'PNG')

# set location
doc = fitz.open(input_file)
page = doc[0]
rect = page.rect
br = rect.br - [50.0, 50.0]
tl = rect.tl + [50.0, 50.0]
image_rectangle = fitz.Rect(tl, br)

for i in range(len(doc)):
	page = doc[i]
	# add the image
	page.insert_image(image_rectangle, stream=imgbuf)

doc.save(output_file, deflate=True)

result image
image

Thanks for your code, Can you help solve this problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions