Add Your Watermark on a Photo using Python

Introduction

You may have noticed many images are watermarked(owner name, brand name, date, etc.) by the creator. It gives a professional look. Not only that, it also represents their intellectual properties.

You can easily add a watermark to a photo using some tools online or offline.

But did you know that the same thing can be done using a few lines of python code? Sounds great, right?

In this tutorial, we will talk about how to Add a Watermark on a Photo using Python. We're gonna use pillow module here, to perform this task.

So, Before we get started, let's take a look at what we've to install.

☛Visit Also: Hack with Image: Extract Metadata from an Image using Python

Requirements

Use pip3 instead of pip for Linux.

☛Install pillow: pip install pillow

Add a Text Watermark on an Image

Here we will add a text watermark to an image. Simply, follow these simple steps described below.

Steps:

1. Import Image, ImageDraw, and ImageFont modules from PIL.

2. Open the image file. In my case, "girl_image.jpg".

3. Call the Draw function from the ImageDraw module by passing the image as an argument to the function.

4. Choose a font type.

5. Select the coordinate, where you want to add the text on the image.

6. Draw the text on the photo as per the coordinate.

7. Now, save the photo with a name as per your choice.

Remove the comment(#) from the yellow marked line if you're running a Linux machine. Otherwise, keep the code as it is.

The Code


from PIL import Image, ImageDraw, ImageFont

img = Image.open('girl_image.jpg')
# Getting the height and width of the image
width, height = img.size

draw = ImageDraw.Draw(img)
text = "PySeek"

# ********For Ubuntu*********
#font = ImageFont.truetype('Ubuntu-M.ttf', 45)

# ********Default Font*******
# *****Work for everyone*****
# font = ImageFont.load_default()

# **********For Windows*********
font = ImageFont.truetype('arial.ttf', 45)


textwidth, textheight = draw.textsize(text, font)

x = width - textwidth - 10
y = height - textheight - 15

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

# Saving the image
img.save('watermarked.jpg')

# Open the image
img = Image.open('watermarked.jpg')
img.show()

Output

a text watermark is added on this picture using a python program

Important note for Linux User

If you run this code on a Linux system then, you may get an error message like this.

Traceback (most recent call last):
  File "image_watermark.py", line 18, in <module>
    font = ImageFont.truetype('arial.ttf', 45)
  File "/usr/lib/python3/dist-packages/PIL/ImageFont.py", line 642, in truetype
    return freetype(font)
  File "/usr/lib/python3/dist-packages/PIL/ImageFont.py", line 639, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/usr/lib/python3/dist-packages/PIL/ImageFont.py", line 187, in __init__
    self.font = core.getfont(
OSError: cannot open resource


To avoid this error, use the default font or Linux Font. 

To find where the Linux fonts is present, simply run this command: 'locate *.ttf' on the terminal. 

I've commented two lines in the code. If your Linux distribution is Ubuntu then use that.

Ubuntu Font folder looks like this.

Ubuntu font folder - PySeek
Ubuntu Font Folder

Add a Logo(or Image Watermark) to an Image

We added text watermark on a photo successfully. Let's try to add a logo to an image now. In this case, you need to create a perfect size logo previously. Next, you need to mention the path and size(see the yellow marked line) of it to the program  before pasting.

The rest of the steps are same, as mentioned above.

In my case, I made a python logo('logo.png') before. You can try something else too.

The Code


from PIL import Image

img = Image.open('girl_image.jpg')
# Getting the height and width of the image
width, height = img.size

size = (100, 100)
logo = Image.open('logo.png')
# You can use resize method here instead of
# thumbnail method
logo.thumbnail(size)

# Location where we want to paste it on the
# main image
x = width - 100 - 10
y = height - 100 - 15

img.paste(logo, (x, y))
# Save the image
img.save('image_with_logo.jpg')

# Opening the new image
img = Image.open('image_with_logo.jpg')
img.show()

Output

a logo is added on this picture using a python program

Output Video


In this tutorial, we learned How to Add a Watermark on an Image using a Python Program. The watermark would be in a text form or a logo. We tried both examples here.

Try to add your logo using this python program and let me know how it's working.

For any query related to this topic, please leave your comment below. You'll get an immediate response.

Thanks for reading!💙 

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post