# Use Python and OpenCV to watermark the image

2022-01-29 20:40:23 Haiyong

This article has participated in 「 Digging force Star Program 」, Win a creative gift bag , Challenge creation incentive fund .

In this paper , I will take you to use Python and OpenCV Watermark the image . The watermark intentionally leaves text on the image / sign , Many bloggers use watermarks to protect the copyright of images . Using watermark, we can ensure that the owner of the image is the person who printed the watermark on the image .

The picture before the watermark ： logo.png：

## The first 1 Step ： Import OpenCV And read logo And the image to which the watermark is to be applied

``````#  Use  OpenCV  Import  cv2  Watermark image
import cv2

#  Import the... We're going to use logo

#  Import the image we want to apply watermark
Copy code ``````

## The first 2 Step ： 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 want to place the watermark somewhere on the image , So , We just need to know the correct width and height of the logo and image .

``````#  Calculation logo Size, height and width
h_logo, w_logo, _ = logo.shape

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

ad locum , We used OpenCV Medium shape function , It returns a tuple of the height and width of the image .

## The first 3 Step ： Place the watermark in the center of the image

Now? , We will calculate the coordinates of the center of the image , Because I want to place the watermark in the center of the image , You can also choose other locations .

``````#  Calculation center the coordinates of the calculation center , We'll put a watermark in it
center_y = int(h_img/2)
center_x = int(w_img/2)

#  From above 、 Next 、 Right 、 Left calculation
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 code ``````

## The first 4 Step ： Use OpenCV Medium addWeighted function

To add a watermark to an image , We will use OpenCV Medium addWeighted function . First , We will provide the destination where the watermark will be placed , The destination is then passed to the... With the image and logo addWeighted function .

grammar ： `cv2.addWeighted(source1, alpha, source2, beta, gamma)`

In our case ,source1 We want to place logo Image ,alpha yes logo The opacity of ,source2 yes logo In itself , We will set... Accordingly beta by 1, Opacity alpha and gamma Respectively 1 and 0.

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

## The first 5 Step ： Display the results and save the output

after , We just display the results and save the output . To show that we use imshow Function and write / Save image , We use... In both functions imwrite function , First, we must provide the file name as a parameter , Then the file itself .cv2.waitKey(0) Used to wait until the user presses Esc key , after cv2.destroyAllWindows The 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 code ``````

## * Here is the complete implementation ：

``````#  Use  OpenCV  Import  cv2  Watermark image
import cv2

#  Import the... We're going to use logo

#  Import the image we want to apply watermark

#  Calculation logo Size, height and width
h_logo, w_logo, _ = logo.shape

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

#  Calculation center the coordinates of the calculation center , We'll put a watermark in it
center_y = int(h_img/2)
center_x = int(w_img/2)

#  From above 、 Next 、 Right 、 Left calculation
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 picture
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 code ``````

Output ：

I've been writing a technology blog for a long time , And mainly through the Nuggets , This is one of my articles Python and OpenCV Watermark the image . I like to share technology and happiness through articles . You can visit my blog ： juejin.cn/user/204034… To learn more . I hope you will like ！

You are welcome to put forward your opinions and suggestions in the comment area ！

Nuggets officials will be in Digging force Star Program After the event , Draw... In the comment area 100 Around the Nuggets , For details of the lucky draw, see the activity article

https://en.pythonmana.com/2022/01/202201292040224440.html