Protecting Your Pixels with Python — Add Watermark to Images

Himani Bansal
Wiki Flood
Published in
3 min readDec 14, 2023

Watermarking is a text or logo imposed on images to prevent it from being used without your permission. It is used to protect the copyright of images.

The Watermark is generally placed at a position where it does not interrupt in viewing the image. Without Watermark, anyone on the internet can use your images for various purposes.

To prevent this, a Watermark is used, and it can be of any type like a person’s name, company’s name, logo, signature, etc.

About Python Add Watermark to Images

In this Add Watermark to Images using Python Project we will build an application to add text Watermark to any image using Python. We will be using PIL Library which is a built-in library of Python used for image processing.

We will use the tkinter library for creating the interface of application. The image should be in .jpg format.

Add Watermark to Images using Python
Add Watermark to Images using Python

Prerequisites For Add Watermark to Images using Python

First we will install the required libraries in our system using a pip installer.

pip install Pillow
pip install tkinter
pip install os

Importing the Required Libraries in our program:

from tkinter import *
from tkinter import filedialog
import os
from PIL import Image, ImageDraw, ImageFont, ImageTk

PIL- This library is used for image processing in Python.

We will import Image, ImageDraw, ImageFont from PIL.

tkinter- This library will help us in creating a GUI window for our app.

Initializing GUI Window:

root = Tk()
root.geometry("500x500")
root.title("Add Watermark")
root.config(bg='#BCD2EE')

Creating Label, Frame, Entry Fields and Buttons:

Label(root, text="Add Watermark", font=('Arial,bold', 20), bg='#6E7B8B', fg='White', width=45).pack(pady=10)

f1 = Frame(root, width=355, height=280, bg='White', bd=5)
f1.place(x=75, y=70)

lb1 = Label(f1, bg="White")
lb1.place(x=40, y=10)

wtm = StringVar()
en1 = Entry(root, textvariable=wtm, font=('Arial,bold', 15), width=30)
en1.place(x=85, y=370)

btn1 = Button(root, text="Select Image", font=('Arial,bold', 15), bg='#6E7B8B', fg='White', command=openfile)
btn1.place(x=90, y=430)
btn2 = Button(root, text="Add Watermark", font=('Arial,bold', 15), bg='#6E7B8B', fg='White', command=add_wm)
btn2.place(x=240, y=430)

root.mainloop()

Code for selecting the image:

def openfile():
global file
file = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select Image",
filetypes=(("PNG file", "*.png"),
("JPG file", "*.jpg"), ("All Types", "*.txt")))
img1 = Image.open(file)
img = ImageTk.PhotoImage(img1)
lb1.config(image=img, width=250, height=250)
lb1.image = img

mage.open()- This will open the image of the given name.

Code for adding the Watermark:

def add_wm():
wt = en1.get()
img2 = Image.open(file)
width, height = img2.size
draw = ImageDraw.Draw(img2)
text = wt
font = ImageFont.truetype('C:/pythondata/Python Projects/DkHandRegular-orna.ttf', 100)
textwidth, textheight = draw.textsize(text, font)
x = 200
y = 200
draw.text((x, y), text, font=font)
img2.show()
img2.save("watermarked2.jpg")

ImageDraw.draw()- This will help in modifying the image.

ImageFont.truetype()- It is the font style of watermark text.

Here we have downloaded this font for free and it is present in the same folder where this (.py) file is saved.

draw.textsize()- This will set the width and height of the Watermark text.

x,y- They are the coordinates on x-axis and y-axis.

draw.text()- This will place the Watermark on the image at given coordinates.

img.show()- This will display the Watermarked image.

img.save()- This will save the image in the system with the given name.

Python Add Watermark to Images Output

Add Watermark to Images using Python Output
Add Watermark to Images using Python Output
Add Watermark to Images Output
Add Watermark to Images using Python
Adding  Watermark to Images using Python
Adding Watermark to Images using Python

Conclusion

We have successfully learned how to add Watermark to any image using the Python Program. We used the PIL library for image processing. Now you can add your own watermark to your images using this Python Program.

--

--

Himani Bansal
Wiki Flood

Doing my Best to Explain Data Science (Data Scientist, technology freak & Blogger)