How to watermark Image with Text using Python

Image Watermark

In this tutorial I am going to show you an example on how to watermark image with text using Python. You might have seen somewhere online portal or social network portals that some images are watermarked, because the owners of the images want to prevent misuse of the images.

Here I am going to show you how you can place the text watermark on different places, for example: right bottom, center, on top of the image. I will also show how to change the color of the watermarked text on top of the image. I am going to use Pillow library for implementing text watermark on the images.

Prerequisites

Python 3.9.1/3.11.5, Pillow Module (pip install pillow) 9.4.0/10.0.0

Watermark Images

Either you can watermark individual image or multiple images one by one reading from a directory. You can also apply separate text for watermarking individual image.

In this example, I am going to read images from a particular directory and applying the same text for watermarking all images one by one.

The following code will work for pillow version 9.4.0.

import os, sys
from PIL import Image, ImageDraw, ImageFont

img_dir = "images/non-watermark/"
dirs = os.listdir( img_dir )

for img in dirs:
	if os.path.isfile(img_dir + img):
		#Create an Image Object from an Image
		im = Image.open(img_dir + img)
		
		#Image width and height
		width, height = im.size
		
		#Image name
		img_name = os.path.basename(img_dir + img)
		
		#print(img_name)

		text = "{roytuts.com}"
		font = ImageFont.truetype('arial.ttf', 30)
		
		draw = ImageDraw.Draw(im)
		
		textwidth, textheight = draw.textsize(text, font)

		#Right bottom corner with margin 5 from right
		margin = 5
		#x = width - textwidth - margin
		#y = height - textheight - margin
		
		#Center of the image
		x = (width - textwidth)/2 #center
		y = (height - textheight)/2 #center
		
		draw.text((x, y), text, font=font)
		#im.show() //Will display in the image window
		
		#Save watermarked image
		im.save('images/watermark/' + img_name)

First of all I have imported the required modules from the Pillow (PIL) library. The images I am going to read from a directory images/non-watermark/.

Next I am iterating through each image, opening the image, getting width & height of the image, name of the image without full path.

The text {roytuts.com} which will be put on the image as a watermark. I am also specifying the font with size for this watermark text.

Next drawing the text area on the image using draw object.

I am calculating the coordinate of x and y position which is basically right bottom corner with margin 5 using the following code snippets:

margin = 5
x = width - textwidth - margin
y = height - textheight - margin

Then I am drawing the text on the image using the following line of code:

draw.text((x, y), text, font=font)

Finally saving the watermarked images in the directory images/watermark/.

im.save('images/watermark/' + img_name)

For pillow version 10.0.0, the following code should be used:

import os, sys
from PIL import Image, ImageDraw, ImageFont

img_dir = "images/non-watermark/"
dirs = os.listdir( img_dir )

for img in dirs:
	if os.path.isfile(img_dir + img):
		#Create an Image Object from an Image
		im = Image.open(img_dir + img)
		
		#Image width and height
		width, height = im.size
		
		#Image name
		img_name = os.path.basename(img_dir + img)
		
		#print(img_name)

		text = "{roytuts.com}"
		font = ImageFont.truetype('arial.ttf', 30)
		
		draw = ImageDraw.Draw(im)
		
		_, _, textwidth, textheight = draw.textbbox((0, 0), text, font)

		#Right bottom corner with margin 5 from right
		margin = 5
		#x = width - textwidth - margin
		#y = height - textheight - margin
		
		#Center of the image
		x = (width - textwidth)/2 #center
		y = (height - textheight)/2 #center
		
		#draw.text((x, y), text, font=font)
		draw.text((x, y), text, font=font, fill=(254, 130, 75, 15))
		#im.show() //Will display in the image window
		
		#Save watermarked image
		im.save('images/watermark/' + img_name)

Testing the Watermark App

Now executing the above code will give you the following watermarked images:

watermark image with text using python

Now if you want to put the text in center on top of the image then you can calculate the coordinate as below:

x = (width - textwidth)/2
y = (height - textheight)/2

Therefore you will get the following output on the image:

watermark image with text using python

If you want to change the color of the watermarked text, then you have to change the line of code draw.text((x, y), text, font=font) to:

draw.text((x, y), text, font=font, fill=(254, 130, 75, 15))

The output you will see on the image is:

watermark image with text using python

That’s all about watermarking images with text using Python and Pillow library.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *