How To Add Textual Watermarks To The Images With OpenCv and PIL!

Mrinal Singh Walia 19 Apr, 2023 • 6 min read
This article was published as a part of the Data Science Blogathon
 Textual Watermarks image 1

Photo by Markus Spiske on Unsplash


Hi, today we are going to discover how to attach watermark text to an image. We will practice OpenCV & PIL to implement various varieties of image manipulations. We make this tutorial very straightforward to follow.

This post will assist you in acquiring knowledge regarding computer vision techniques employing Python programming. Here I will explain how to execute computer vision functions and implement them in different aspects, practicing some prominent examples. Then we will reflect the output along with the illustrations.

Lastly, we will also address the fundamental image processing and implement a specific description of computer vision functions.

Aims: The aim is to explain how to draw logos or watermark on photographs applying two different approaches in computer vision working with python – OpenCV and PIL.

Points Included In This Article:

  • Producing watermark from text utilizing OpenCV
  • Producing watermark from text utilizing PIL


What Is A Digital Watermark?

Watermark is information that may be a logo, trademark, or stamp applied to identify the creator’s ownership. It reflects the logo without interpreting the clarity of the photograph. Before distributing their content on public platforms, most companies and professionals restrict other people from copying it by practicing Watermarks.

Purpose Of Watermarking

People may implement digital watermarking on countless applications, such as:

  • Copyright iD
  • Source tracking (various recipients get adversely watermarked content)
  • User authentication or fingerprint
  • Tamper detection
  • Image protection

Difference Between Pillow & OpenCV For Computer Vision Tasks

Both OpenCV & Pillow are one of the best in their terms. Both provide a comprehensive set of methods to work with your videos, photos and many more. Let us learn the essential contrast between the two:

  • Type: Pillow is an image manipulation & processing library, whereas OpenCV is a computer vision library.
  • Runtime: Pillow is slow in comparison to OpenCV, which is much faster in runtime.
  • Method To Open An Image: In pillow we use wherein OpenCV we use cv2.imread(file_path).
  • Image Type Supported: In pillow PIL format & in OpenCV numpy.ndarray type format is preferred.
  • How To Display Images: In pillow we use whereas in OpenCV we use cv2.imshow(“Name_OF_Image”, image).
  • How To Save Images: In the pillow we use, “JPEG”) whereas in OpenCV we use cv2.imwrite(“output_img.png”, image).
  • Converting Image To Array: In pillow, we can apply image.fromarray() to convert an input image to an array of features, whereas in OpenCV, the input image is already in NumPy array format.
  • How To Resize: In pillow we can resize an image by using image.resize((256, 256), Image.CUBIC), in OpenCV do the same as follows: cv2.resize(image, dsize=(256, 256), interpolation=cv2.INTER_CUBIC).
  • How To Flip An Image: To flip an image, we use image.transpose(Image.FLIP_LEFT_RIGHT) in pillow whereas in OpenCV we do the same by cv2.flip(image, flipCode=1).
  • How To Blur An Image: To blur an image, we use ImageOps.box_blur(image, radius=2) in pillow whereas in OpenCV we do the same by cv2.blur(image, ksize=(3, 3)).

OpenCV incorporates most of everything PIL offers and more extra. If you only require any library for image processing and straightforward tasks, exercise PIL; otherwise, OpenCV should be your prime choice.

Requirements & Specifications:

    • Numpy library (pip install numpy)
    • OpenCV library (pip install opencv-python)
    • Python (Link to install python)
    • matplotlib library (pip install matplotlib)
    • PIL library (pip install Pillow)

Before moving to understand how to attach a watermark text to your photographs; let’s see how it will look like on pictures:

  • Main idea: ‘mrinal.jpg’
main image | Textual Watermarks
  • Adding a watermark text “MW”, the photograph will look like:
adding Textual Watermarks with opencv


Producing watermark Text Utilizibg a picture

#1. Applying OpenCV:

OpenCV is a free open source library practiced in real-time image processing. It’s used to process images, videos, and live streams, but we will only prepare pictures as direct action in this tutorial.


First importing all essential libraries and then loading the picture you would wish to watermark. We store the picture applying cv2.imread(). OpenCV renders images as BGR. So we require to transform it into an RGB channel. Unless the Blue and Red channels will be viewed as exchanged when you run plt.imshow().

import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('a.jpg' )
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
rgb image

First, we will practice NumPy to generate a blank image of the identical size to the image we desire to watermark. Following, we accept the OpenCV’s putText function to print our decided text, mentioning font specifications on this blank image. The org attribute is origin coordinates x, y of the source of the text. Mark the flip of x and y with the picture shape when deciding the ranges of text position.

blank = np.zeros(shape=(img_rgb.shape[0],img_rgb.shape[1],3), dtype=np.uint8)
            text='M W',  
            org=(img_rgb.shape[1]//8, img_rgb.shape[0]//2),   
            fontScale= 2,color=(163,163,163),  


blank image Textual Watermarks

With OpenCV drawing functions, you can produce multiple different watermarks – your creativity is the boundary. Next, blend the images applying cv2.addWeighted() function that exerts the latter format: image1 * alpha + image2 * beta + gamma

At last, we demonstrate the picture applying cv2.imshow()

blend = cv2.addWeighted(src1=img_rgb,alpha=0.7,src2=blank,beta=1, gamma = 0)  


 Textual Watermarks mw using PIL

#2. Applying PIL:

Applying the PIL library, we will provide our picture as input and a text to present as a watermark on the photograph. By specifying a value to the margin, we can modify the location of the text in the photo.


First, we import the needed PIL library and packages. Then we load the input picture we want to paint the watermark. Then we get the width and height of the input picture. Dimensions are used to place the text later in the code.

from PIL import Image, ImageDraw, ImageFont
orig_img ='a.jpg')
w, h = orig_img.size

Apply any text you desire to add as a watermark, and we practice the ImageDraw module to produces simple 2D graphics for Drawing objects. We are applying PIL.ImageDraw.Draw() function to generate a thing that can be employed to draw in the given picture.

draw = ImageDraw.Draw(orig_img)
txtw, txth = draw.textsize(txt)
margin = 10

Set the x & y coordinates of the text you desire to attach and draw the text on the primary image and practice to represent the output image and to store the picture locally.

x = w - txtw - margin
y = h - txth - margin
draw.text((x, y), txt)'RESULT.png')


final image


The above article explained how to combine text as a watermark to our photographs employing Python’s OpenCV library & PIL library. Understanding image watermarking is necessary for every CV developer and researcher because it assists as a central element for fulfilling numerous image processing tasks. Furthermore, there are countless different applications where we can implement a visible watermarking system for real-world problems. Now it’s up to you to develop the method for your future applications, and I cannot abide to hear from you.

Thanks for Browsing my Article. Kindly comment and don’t forget to share this blog as it will motivate me to deliver more quality blogs on ML & DL-related topics. Thank you so much for your help, cooperation, and support!

About Author

Mrinal Walia is a professional Python Developer with a computer science background specializing in Machine Learning, Artificial Intelligence and Computer Vision. In addition to this, Mrinal is an interactive blogger, author, and geek with over four years of experience in his work. With a background working through most areas of computer science, Mrinal currently works as a Testing and Automation Engineer at Versa Networks, India. My aim to reach my creative goals one step at a time, and I believe in doing everything with a smile.

Medium | LinkedIn | ModularML | DevCommunity | Github

The media shown in this article are not owned by Analytics Vidhya and are used at the Author’s discretion.

Mrinal Singh Walia 19 Apr 2023

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers


Computer Vision
Become a full stack data scientist