• sreshtha4

Image Watermarking Using Computer Vision



We will create an image watermarking application in this tutorial. Before we get into the specifics, let’s look at how they can be applied in business. When it comes to the importance of image watermarking, it plays a significant role in protecting documents from unauthorized use (copyright infringement). Furthermore, if the correct pipeline is in place for the same application, it saves a significant amount of time because there will be a large number of documents to be watermarked.

Use Case of Image Watermarking

Though we are building an application to watermark any image, do we know of any business use-case for it? So let's first look at that:

  1. Adding the watermark helps in maintaining the legal rights of your company so that other people or organizations won't use it without any concern, and no copyright issue should strike.

  2. This application will also aid in automation, as we all know that manually adding a watermark to each image would take a long time. As a result, this application will assist in automating the entire process, saving a significant amount of time.

Note: Before moving forward to the coding part, I'm using the Jupyter notebook for writing the code for the following reasons:

  1. We can run every chunk of code separately, which will let us understand what each chunk of code is doing without any kind of debugging.

  2. Code + comment approach i.e., we can write code and at the same time document that code as well. Having said that, you are free to use any code editor of your own choice (the Jupyter notebook is preferable).

Firstly, we will be importing all the required libraries.

# Important library import
import cv2
import numpy as np
import request
from PIL import Images

Let's go over how we will use each of the libraries we've imported.

  1. cv2: This library is responsible for importing all of the built-in functions that are supported by computer vision and performing all image processing operations.

  2. NumPy: This library is imported for doing some required mathematical calculations and some color conversion

  3. requests: this library will be used to retrieve images from the internet so that we can use them to our specifications.

  4. PIL: also known as the pillow library, this will assist us in opening the image we will download from the internet.

For this project, we need an image URL on which we will apply a watermark. We'll use the image's URL from Google Images to load it. You can use any image you want and replace the image's URL with the current URL. Next, we will read and resize the image before storing it in two variables for logo and text watermarking.

# Reading image form url
image = Image.open(requests.get('https://wallpaperbat.com/img/515088-pureoled-black-wallpaper.jpg', stream=True).raw)
image_logow = image.resize((500,300))
image_textw = image.resize((500,300))
image_logow
# https://media.sproutsocial.com/uploads/2017/02/10x-featured-social-mediaimage-size.png                                                                                                                                    
# Reading logo form url
logow = Image.open(requests.get('https://www.kark.com/wpcontent/uploads/sites/85/2016/03/cvs_1457227446363_7369586_v.0.png',stream=True).raw)
# logow = logo.resize((209,52))
logow
# https://www.logogenie.net/download/preview/medium/9995302

For the logo, we are using the CVS pharmacy logo which is downloaded using the above URL.

To use these images, we will first convert them into RGB format and store them as a NumPy array, but here the question is, why do we need to convert the image to RGB format? The reason behind this is that RGB has three color channels, which in all combinations give us the colored image instead of the black and white image. Similarly, we will also extract the shape, i.e., its height and width.

image_logow = np.array(image_logow.convert('RGB'))
h_image, w_image, _ = image_logow.shape
logow = np.array(logow.convert('RGB'))
h_logo, w_logo, _ = logow.shape

We will place our watermark in the center of the image, so we need to calculate the center of the image. Here, we will find the center coordinates of the image using the height and width of the image.

# Get the center of the original. It's the location where we will place the watermark
center_y = int(h_image / 2)
center_x = int(w_image / 2)
top_y = center_y - int(h_logo / 2)
left_x = center_x - int(w_logo / 2)
bottom_y = top_y + h_logo
right_x = left_x + w_logo

A Region of Interest is defined as the area we need to place our watermark on. Here, we will find the ROI using the coordinates we found above. Next, we will use OpenCV to merge our logo onto ROI. Similarly, we will also add a line or a pattern to our image.

# Get ROI
roi = image_logow[top_y: bottom_y, left_x: right_x]
# Add the Logo to the Roi
result = cv2.addWeighted(roi, 1, logow, 1, 0)
# Drawing
cv2.line(image_logow, (0, center_y), (left_x, center_y), (0, 0, 255),1)cv2.line(image_logow, (right_x, center_y), (w_image, center_y), (0, 0,255), 1)
# Replace the ROI on the image
image_logow[top_y: bottom_y, left_x: right_x] = result

In the above code, when we will see the drawing section, in the cv2.line function, we can see that there are parameters of (x1,y1),(x2,y2) then the color combination that is also in an RGB format i.e. (0,0,255)which stimulates 0- Red, 0- Green and 255- Blue that means the color which is selected is Blue similarly many combinations of color can be derived just by different combinations of values.

Now we will convert the image from the array to RGB and visualize it.

# Ploting logo watermark image
img = Image.fromarray(image_logow, 'RGB')
img

Next, we shall see how we can use text as a watermark. We will convert our image into RGB and save it as a NumPy array. To put the text as a watermark, we will be using the put text() function of OpenCV. Here we can use different features like the size of the text, thickness of the text, font family, etc.

# Text Watermark
image_text = np.array(image_textw.convert('RGB'))
cv2.putText(image_text, text='CVS pharmacy', org=(w_image - 140, h_image-10) , fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5, color=(100,255,120), thickness=2, lineType=cv2.LINE_4);

In the above code, we can see the put text function, in which one is a text parameter that will hold the text that will be inked on the image as the watermark, then the location of the watermark (where it will be present in the image), and finally the font face parameter, which is responsible for giving different types and formats of text.

To visualize the image we need to convert it back to RGB format.

# Plotting text watermark image
timg = Image.fromarray(image_text, 'RGB')
timg

Precautions

  1. Image quality: Be careful about the quality of the image, as the lower the quality of the image, the less it will affect the watermark.

  2. Location of image and text watermark: Though we have used the standard location for both the image and text watermark, if one wants to change the location, then be careful about that, as if the location provided by you is out of the frame, then nothing will be displayed.

Summary

So, if we want to summarize everything we’ve done so far, here it is:

  1. We imported all of the libraries needed to build this application.

  2. Read the image on which we wanted to watermark the logo and name of the company.

  3. After that, we applied all the necessary image processing techniques, such as changing the color. In short, the image’s format can be changed to write text and place a logo on the image of our choice. Everything we did with the image was done using image processing techniques.

  4. Then, in the end, we saw the output, which assisted us in resolving the issue of making the secure in the sense that it cannot be used without a license or a paid version. As a result, the automation component saves time.

By : Aman Preet Singh Gulati

23 views0 comments