In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use Python and OpenCV to watermark the image", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Python and OpenCV to watermark an image.
Preface
In this article, I will take you to use Python and OpenCV to watermark the image. The watermark intentionally leaves text / logo on the image, and many bloggers use the watermark to protect the copyright of the image. Using the watermark, we can ensure that the owner of the image is the person who prints the watermark on the image.
The picture before the watermark:
Logo.png:
? Step 1: import OpenCV and read logo and the image to which you want to apply watermark
# use OpenCV to import the watermark image of cv2 import cv2# import the logologo = cv2.imread ("logo.png") we will use # Import the image img = cv2.imread ("haiyong.png") to which we want to apply the watermark? 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 want to place the watermark somewhere on the image, and to do this, we just need to know the correct width and height of the logo and image.
# calculate the dimension height and width of logo h_logo, w_logo, _ = logo.shape# image height and width h_img, w_img, _ = img.shape
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're going to calculate the coordinates of the center of the image, because I'm going to place the watermark in the center of the image, and you can choose another location.
# the coordinates of the computing center, in which we will place the watermark center_y = int (h_img/2) center_x = int (w_img/2) # calculate top_y = center_y-int (h_logo/2) bottom_y = top_y + h_logoright_x = left_x + w_logoleft_x = center_x-int (w_logo/2) from top, bottom, right and left? 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 where we want to place the watermark, 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 where we want to place logo, alpha is the opacity of logo, source2 is logo itself, and we will set beta to 1, alpha and gamma to 0, respectively.
# add watermark destination = IMG [top _ y:bottom_y, left_x:right_x] result = cv2.addWeighted (destination, 1, logo, 1, 0) to the picture? Step 5: display the results and save the output
After that, we just display the results and save the output. To display the output we use the imshow function and write / save the image, we use the imwrite function in both functions, first we must provide the file name 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 imageimg [top _ y:bottom_y, left_x:right_x] = resultcv2.imwrite ("watermarked.jpg", img) cv2.imshow ("Watermarked Image", img) cv2.waitKey (0) cv2.destroyAllWindows ()
? Here is the complete implementation:
# Import the watermark image of cv2 using OpenCV import cv2# import the watermark image we will use logologo = cv2.imread ("logo.png") # import the image we want to apply watermark img = cv2.imread ("haiyong.png") # calculate the dimension height and width of logo h_logo, w_logo, _ = logo.shape# image height and width h_img, w_img, _ = the coordinates of the img.shape# computing center We will put watermark center_y = int (h_img/2) center_x = int (w_img/2) # calculate top_y = center_y-int (h_logo/2) left_x = center_x-int (w_logo/2) bottom_y = top_y + h_logoright_x = left_x + w_logo# to add watermark destination = IMG [top _ y:bottom_y] to the picture from top, bottom, right and left Left_x:right_x] result = cv2.addWeighted (destination, 1, logo, 1, 0) # display and save image img [top _ y:bottom_y, left_x:right_x] = resultcv2.imwrite ("watermarked.jpg", img) cv2.imshow ("Watermarked Image", img) cv2.waitKey (0) cv2.destroyAllWindows ()
Output:
At this point, I believe you have a deeper understanding of "how to use Python and OpenCV to watermark the image". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.