Watermark Images: Image Processing - Opencv, Python & C++ By: Rahul Kedia
Watermark Images: Image Processing - Opencv, Python & C++ By: Rahul Kedia
Source Code:
https://github.com/KEDIARAHUL135/WatermarkImages.
git
1
Table of Contents
Overview 3
Watermarking Images 7
2
Overview
Have you ever wondered why there is a name or a logo on images over the internet? These
are generally not easily visible and are present usually at a corner or all over the image.
These are known as Watermarks. These denotes usually the name of the individual/firm
that owns the image thus helps to preserve the copyright over the image. In this part, we’ll
get to know how we can add our own watermark to our images.
DISCLAIMER: The input images used in this project are not owned by KnowInDeep
however are watermarked for demonstration purposes. The images are downloaded from
freepik.com.
3
Setting up initial code
Before moving to the main source code, let us first write a code that reads the input images
and the watermark image, pass them to a function for watermarking and finally stores the
resultant watermarked images.
Here, input images can be BGR/RGB images of any dimentions, and the watermark image is
a single channeled image of the logo with the logo in “white” colour (value = 255) and the
background in “black” colour (value = 0) as shown below.
import os
import cv2
import numpy as np
The cv2 and numpy libraries will be used for image processing throughout whereas the os
library is used while reading the input images.
Now let us define a function that reads and stores the input images and their names in order
using the path of the folder/file provided.
def ReadImage(ImagePath):
# Input Images will be stored in this list.
Images = []
# Names of input images will be stored in this list.
ImageNames = []
4
InputImage = cv2.imread(ImagePath)
# Storing the image.
Images.append(InputImage)
# Storing the image's name.
ImageNames.append(os.path.basename(ImagePath))
This is an easy-to-understand code explained with the help of comments. Firstly, we check if
the path provided is of a file or folder and accordingly read the images and their names and
store them in respective lists.
Now, let’s call this function from the main function and pass the images for processing.
if __name__ == "__main__":
# Setting input and output image paths
InputImagePath = "InputImages"
OutputImagePath = "OutputImages_py"
# Reading Images
Images, ImageNames = ReadImage(InputImagePath)
Watermark = cv2.imread("kid_logo.jpg", cv2.IMREAD_GRAYSCALE)
for i in range(len(Images)):
Image = Images[i]
5
# Storing the final output images
cv2.imwrite(OutputImagePath + "/" + ImageNames[i],
WatermarkedImage)
Here, firstly the path of the input images’ folder (images will be read from here) and output
images’ folder (watermarked images will be stored here) is provided. After that, the input
images are read using the previously defined function, and also the watermark image is read
in grayscale format. Then in the for loop, these images are passed one by one along with the
watermark image to a function for watermarking and the resultant images are stored with the
same name inside the output images’ folder.
Now let’s have a look at the main code that applies watermark on the images.
6
Watermarking Images
Before actually watermarking, let us first resize our watermark image in accordance with the
input image so that after watermarking, the watermark looks uniform on all the images and is
not too big or too small. You can however skip this step or modify it according to your need.
Here, I am resizing the watermark image such that the watermark’s height is approximately
10% of the input image’s height. The width is calculated accordingly keeping the aspect ratio
the same.
We’ll be now creating two 3-channeled images. One will be our watermark image (we earlier
had a single channeled image) and the other one will be denoting the transparency level for
each pixel of the watermark (this image will be a float type having values between 0.0-0.1).
Here, for the Alpha image, we are multiplying all pixel values by 0.4 so that we get the
transparency of 0.6 (60%).
NOTE: In the Alpha Image, the values are first divided by 255 to bring the pixel’s value in the
range of 0.0 - 0.1.
Now let us finally add this watermarked image to the initial input image using the
transparency values (Alpha). We will add the watermark on the bottom right corner of the
image leaving 20 pixels from both boundaries.
Such addition of the images is known as image blending. OpenCV has a function that
directly takes in the 2 images to be added and the alpha image and returns the output
image. This function is cv2.addWeighted(). You can read about this function here. But, here
7
we will be applying the general weighted addition of images without using the addWeighted()
function.
You can change some params like the position of the watermark, the size ratio of the
watermark, or the transparency of the watermark and get your desired output.
8
9