Watermark Photos with Python

GeoSense ✅
2 min readNov 25, 2022

A watermark is usually some text or a logo overlaid on the photo that identifies who took the photo or who owns the rights to the photo.

To watermark photos using Python, you can use the Python Imaging Library (PIL) or OpenCV library. Both libraries provide tools for image processing and can be used to add a watermark to an image.

The first thing you need to do is install Pillow if you haven’t already:

pip install pillow
Source: Author photo MG_5329.JPG

Adding a Text Watermark

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont


def watermark_text(input_image_path,
output_image_path,
text, pos):
photo = Image.open(input_image_path)

# make the image editable
drawing = ImageDraw.Draw(photo)

black = (3, 8, 12)
# font = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 40)
font = ImageFont.truetype("arial.ttf", 40)
# font = ImageFont.load_default()
drawing.text(pos, text, fill=black, font=font)
photo.show()
photo.save(output_image_path)


if __name__ == '__main__':
img = 'IMG_5329.JPG'
watermark_text(img, 'MG_5329.JPG_text.jpg',
text='TNMTHAI.com',
pos=(0, 0))

Here’s the result:

Watermark an Image with Transparency

from PIL import Image

def watermark_with_transparency(input_image_path,
output_image_path,
watermark_image_path,
position):
base_image = Image.open(input_image_path)
watermark = Image.open(watermark_image_path)
width, height = base_image.size

transparent = Image.new('RGBA', (width, height), (0,0,0,0))
transparent.paste(base_image, (0,0))
transparent.paste(watermark, position, mask=watermark)
transparent.show()
transparent.save(output_image_path)


if __name__ == '__main__':
img = 'IMG_5329.jpg'
watermark_with_transparency(img, 'IMG_5329_logo.png',
'logo.png', position=(0,0))

Here’s the result:

Pretty cool, eh?

Is the logo too big? we can resize it by the following code:

    watermark = Image.open(watermark_image_path)
watermark= watermark.resize((150, 169))

Thank you very much.

--

--

GeoSense ✅

🌏 Remote sensing | 🛰️ Geographic Information Systems (GIS) | ℹ️ https://www.tnmthai.com/medium