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)
Thanks for your code, Can you help solve this problem.