How to add a text or image as a watermark behind the pdf file? #2175
-
Hi community, I am using PyMuPDF to merge my pdf files, and add watermark in my merged pdf using Thanks for helping |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
An image's transparency / opacity is a property of the image, and there is nothing in PDF that lets you display an image transparently if the image per se is not transparent. There are ways however to add transparency (i.e., alpha channel) to an image in PyMuPDF, and also to set the single values of each transparency byte. Then insert the modified image. pix = fitz.Pixmap(<image>) # could be some image file, or an image in memory
# check if it has an alpha channel, if not add one
if not pix.alpha:
pix = fitz.Pixmap(pix, 1) # alpha channel added
# number of pixels
pixel_count = pix.width * pix.height
# prepare new alpha values for 30% transparency:
# 0.3 * 255 = ca. 76
alphas = bytes([76]*pixel_count)
pix.set_alpha(alphas) then insert the pixmap as image. |
Beta Was this translation helpful? Give feedback.
An image's transparency / opacity is a property of the image, and there is nothing in PDF that lets you display an image transparently if the image per se is not transparent.
There are ways however to add transparency (i.e., alpha channel) to an image in PyMuPDF, and also to set the single values of each transparency byte. Then insert the modified image.
This process incurs using the
Pixmap
class and roughly work along this snippet: