This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

In this article, I’ll walk you through watermarking images using Python and OpenCV. Watermarks intentionally leave text/logos on images and are used by many bloggers to protect image copyright. Using watermark we can ensure that the owner of the image is the person who printed the watermark on the image.

Image before watermarking:Logo. PNG:

🌌 Step 1: Import OpenCV and read the logo and the image to be watermarked

# Use OpenCV to import cV2 watermark image
import cv2

Import the logo we will use
logo = cv2.imread("logo.png")

Import the image to which we want to apply the watermark
img = cv2.imread("haiyong.png")
Copy the code

💨 Step 2: Calculate the height and width of the two images

Calculate the height and width of the two images and save them to other variables. We need to calculate the width and height because we are going to place the watermark somewhere on the image, and for that we only need to know the correct width and height of the logo and image.

Calculate the size, height and width of the logo
h_logo, w_logo, _ = logo.shape

# Height and width of the image
h_img, w_img, _ = img.shape
Copy the code

Here, we use the Shape function in OpenCV, which returns a tuple of the height and width of the image.

🚀 Step 3: Place the watermark in the center of the image

Now, we will calculate the coordinates of the center of the image, because I will place the watermark in the center of the image, and you can choose any other location.

Compute center Compute the coordinates of the center in which we will place a watermark
center_y = int(h_img/2)
center_x = int(w_img/2)

# Count from top, bottom, right, left
top_y = center_y - int(h_logo/2)
bottom_y = top_y + h_logo
right_x = left_x + w_logo
left_x = center_x - int(w_logo/2)
Copy the code

🍺 Step 4: Use the addWeighted function in OpenCV

To add a watermark to the image, we will use the addWeighted function in OpenCV. First, we will provide the destination to place the watermark on, and then pass that destination to the addWeighted function with the image and logo.

Syntax: cv2.addWeighted(source1, alpha, source2, beta, gamma)

In our example, source1 is the image we want to place the logo on, alpha is the opacity of the logo, and source2 is the logo itself. We will set beta to 1 and the opacity alpha and gamma to 1 and 0, respectively.

Add a watermark to the image
destination = img[top_y:bottom_y, left_x:right_x]
result = cv2.addWeighted(destination, 1, logo, 1.0)
Copy the code

🎨 Step 5: Display the results and save the output

After that, we just display the results and save the output. To display our output using the imshow function and write/save the image, we use the imwrite function in both functions, first we have to supply the filename as an argument, and then the file itself. Cv2.waitkey (0) is used to wait until the user presses the Esc key, after which the cv2.DestroyallWindows function closes the window.

# displaying and saving image
img[top_y:bottom_y, left_x:right_x] = result
cv2.imwrite("watermarked.jpg", img)
cv2.imshow("Watermarked Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

🛹 Here is the full implementation:

# Use OpenCV to import cV2 watermark image
import cv2

Import the logo we will use
logo = cv2.imread("logo.png")

Import the image to which we want to apply the watermark
img = cv2.imread("haiyong.png")

Calculate the size, height and width of the logo
h_logo, w_logo, _ = logo.shape

# Height and width of the image
h_img, w_img, _ = img.shape

Compute center Compute the coordinates of the center in which we will place a watermark
center_y = int(h_img/2)
center_x = int(w_img/2)

# Count from top, bottom, right, left
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

Add a watermark to the image
destination = img[top_y:bottom_y, left_x:right_x]
result = cv2.addWeighted(destination, 1, logo, 1.0)

Display and save images
img[top_y:bottom_y, left_x:right_x] = result
cv2.imwrite("watermarked.jpg", img)
cv2.imshow("Watermarked Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Copy the code

Output:

I’ve been writing a technical blog for a long time, mostly through nuggets, and this is one of my posts about watermarking images using Python and OpenCV. I like to share technology and happiness through articles. You can visit my blog at juejin.cn/user/204034… For more information. Hope you like it! 😊

💌 welcomes your comments and suggestions in the comments section! 💌

The nuggets will be drawing 100 nuggets in the comments section after project Digital. see the event article for details