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:
Note: Before moving forward to the coding part, I'm using the Jupyter notebook for writing the code for the following reasons:
Firstly, we will be importing all the required libraries.
# Important library importimport cv2import numpy as npimport requestfrom PIL import Images
Let's go over how we will use each of the libraries we've imported.
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
We should get the following output?:
# Reading logo form url
logow = Image.open(requests.get('https://www.kark.com/wpcontent/uploads/sites/85/2016/03/cvs_1457227446363_7369586_ver1.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 placethewatermark
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
Summary
So, if we want to summarize everything we've done so far, here it is: