[go: up one dir, main page]

0% found this document useful (0 votes)
442 views9 pages

Watermark Images: Image Processing - Opencv, Python & C++ By: Rahul Kedia

The document discusses how to add watermarks to images using OpenCV and Python. It explains setting up the initial code to read input images and a watermark image. It then describes resizing the watermark, creating alpha channels, and blending the watermark onto the input images using weighted addition at a specified corner location with a given transparency level.

Uploaded by

Kedia Rahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
442 views9 pages

Watermark Images: Image Processing - Opencv, Python & C++ By: Rahul Kedia

The document discusses how to add watermarks to images using OpenCV and Python. It explains setting up the initial code to read input images and a watermark image. It then describes resizing the watermark, creating alpha channels, and blending the watermark onto the input images using weighted addition at a specified corner location with a given transparency level.

Uploaded by

Kedia Rahul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Watermark Images

Image Processing - OpenCV, Python & C++

By: Rahul Kedia

Source Code:
https://github.com/KEDIARAHUL135/WatermarkImages.
git

1
Table of Contents

Overview 3

Setting up initial code 4

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.

Fig: KnowInDeep logo in black and white

Let’s begin with importing the libraries:

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 = []

# Checking if path is of file or folder.


# If path is of file.
if os.path.isfile(ImagePath):
# Reading the image.

4
InputImage = cv2.imread(ImagePath)
# Storing the image.
Images.append(InputImage)
# Storing the image's name.
ImageNames.append(os.path.basename(ImagePath))

# If path is of a folder contaning images.


elif os.path.isdir(ImagePath):
# Getting all image's name present inside the folder.
for ImageName in os.listdir(ImagePath):
# Reading images one by one.
InputImage = cv2.imread(ImagePath + "/" + ImageName)
# Storing images.
Images.append(InputImage)
# Storing image's names.
ImageNames.append(ImageName)

# If it is neither file nor folder(Invalid Path).


else:
print("\nEnter valid Image Path.\n")

return Images, ImageNames

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]

# Passing Image for watermarking


WatermarkedImage = WatermarkImage(Image, Watermark)

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.

# Resizing watermark image keeping the aspect ratio same


# Resize is done such that watermark's height is equal to
# 10% of the image's height
NewHeight = int(Image.shape[0]*0.1)
NewWidth = int(NewHeight * (Watermark.shape[1]/Watermark.shape[0]))
Watermark = cv2.resize(Watermark, (NewWidth, NewHeight),
interpolation=cv2.INTER_AREA)

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).

# Creating 3 channeled watermark image and alpha image


# (range -> [0.0-1.0])
Watermark = cv2.merge((Watermark, Watermark, Watermark))
# Transparency of the watermark is 60% (0.4 is opacity)
Alpha = (Watermark.astype(float) * 0.4)/255

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.

# Applying watermark on the bottom right corner leaving 20 pixels


# from both the boundaries
WatermarkedImage = Image.copy()
ah, aw = Alpha.shape[:2]
WatermarkedImage[-(ah+20):-20, -(aw+20):-20] =
cv2.add(cv2.multiply(Alpha, Watermark, dtype=cv2.CV_64F),
cv2.multiply(1.0-Alpha,
Image[-(ah+20):-20, -(aw+20):-20],
dtype=cv2.CV_64F))
WatermarkedImage = np.uint8(WatermarkedImage)

Finally, we return the WatermarkedImage variable and store the output.

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.

Here are the watermarked output images obtained.

8
9

You might also like