How the script works
- It turns phone photos upright (
ImageOps.exif_transpose()), scales 16-bit images to 8 bits, and opens the result asRGBA, so the watermark can be blended. - It draws the text once on a small transparent image, the stamp, sized with
font.getbbox()so nothing is clipped, and rotates it withexpand=True. - It pastes the stamp, once or in a grid, onto an empty transparent layer the size of the photo.
Image.alpha_composite()blends that layer over the photo, so the opacity you chose is respected exactly.- It saves PNG when the original had transparency (so it is kept), and JPEG otherwise.
The font size is a percentage of the image width, so the same script gives a proportionate mark on a phone photo and on a 6000 px original. Drawing straight onto an RGB image with an RGBA colour would ignore the alpha value; the ImageDraw reference shows why.
Using your own font
The script loads DejaVuSans-Bold.ttf, which ships with matplotlib and most Linux systems and is what this page
previews with. Replace it with the path of any .ttf or .otf file. If Pillow says
cannot open resource, the path is wrong; that page lists where fonts live on
Windows, macOS and Linux.
Logo watermarks
Choose Logo image and add your logo. The script scales it with ImageOps.contain() to a share of the
photo's width (so the mark is proportionate on any photo size), multiplies its alpha channel by the opacity, so soft edges stay
soft, and places or tiles it like the text. A PNG with transparency gives the cleanest result; a JPEG logo works but keeps its
background rectangle. The theory is in pasting a transparent image.