In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use OpenCV to watermark images", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use OpenCV to watermark images" this article.
1. What is a watermark?
A watermark is a logo, signature, text or pattern deliberately superimposed on different images to protect the copyright of the image.
Its main purpose is to promote the brand and make it more difficult to copy or use the original image without the owner's permission.
Organizations and professionals often use watermarks to prevent their content from being used by others after hosting it online.
So, have you ever thought of adding a watermark to the image?
For example, we write a blog to explain the source of external images. But what about the images you created yourself? Wouldn't it be nice to leave your mark on the image you created?
That's great! Let's begin this exciting task.
two。 Resize the image in OpenCV
Resizing is simply scaling the image, which means changing the size of the original image. We can increase or decrease the size of the image according to the business needs.
You can resize in a variety of ways.
1. Maintain the aspect ratio. The aspect ratio of an image is the ratio of its width to height.
. Reduce or enlarge the size of the image
1. Do not retain aspect ratio
. Reduce / enlarge only the width, only the height
1. Change width and height to specific values
It sounds good so far, but how do we actually do it? The answer is OpenCV and its resize () function. Read more about the OpenCV resizing feature in this document: https://docs.opencv.org/3.4/da/d54/group__imgproc__transform.html#ga47a974309e9102f5f08231edc7e7529d
* * syntax of the cv2.resize () function: * * cv2.resize (src, dsize,interpolation)
Src-Source Ima
Dsize-the required size of the output image
Interpolation-interpolation, Wikipedia definition: it is a method of building (finding) new data points based on the range of a set of discrete known data points.
Refer to this document for more information about interpolation flags: https://docs.opencv.org/3.4/da/d54/group__imgproc__transform.html#ga5bb5a1fea74ea38e1a5445ca803ff121
Now, let's take a sample image and resize it. Here is our example image.
Now, try to display it using OpenCV.
Import cv2img = cv2.imread ('images/deer.JPG') cv2.imshow ("Original Image", img) cv2.waitKey (0) cv2.destroyAllWindows ()
Here is what our image looks like when it is displayed using OpenCV.
Because our image is high-resolution, only part of it is visible.
Therefore, it must be necessary to resize it. We need to reduce its size.
To resize an image in OpenCV:
Use cv2.imread () to read the image
Set the new width and height.
Create a tuple for the new dimension
Resize the image using cv2.resize ()
If necessary, use cv2.imwrite () to save the adjusted image to your computer
Use cv2.imshow () to display the original, resized image
1. Maintain aspect ratio-reduces the image to 20% of its original size.
We reduced the size of the original image to 20% of its original size. Therefore, create a tuple for the new size by calculating 20% of the original width and 20% of the original height.
Import cv2img = cv2.imread ('images/deer.JPG') percent_of_scaling = 20new_width = int (img.shape [1] * percent_of_scaling/100) new_height = int (img.shape [0] * percent_of_scaling/100) new_dim = (new_width, new_height) resized_img = cv2.resize (img, new_dim, interpolation=cv2.INTER_AREA) filename =' resized_img_aspect ratio.jpg'cv2.imwrite (filename, resized_img) cv2.imshow ("Original Image") Img) cv2.imshow ("Resized Image", resized_img) cv2.waitKey (0) cv2.destroyAllWindows ()
The above code saves the resized image and displays the original, resized image.
Well done. By keeping in mind the aspect ratio, we successfully resized the image.
two。 Do not retain aspect ratio-only reduce / enlarge width, only reduce / enlarge height
The steps for resizing are the same as above. The only difference is that we keep either of the two dimensions the same.
Import cv2img = cv2.imread ('images/deer.JPG') new_dim = (img.shape [1], # changes heightresized_img = cv2.resize (img, new_dim, interpolation=cv2.INTER_AREA) cv2.imshow ("Original Image", img) cv2.imshow ("Resized Image", resized_img) cv2.waitKey (0) cv2.destroyAllWindows ()
The following is the image shown from the code above.
The resized image is distorted and is not the desired output.
3. Change width and height to specific values
Import cv2img = cv2.imread ('images/deer.JPG') new_dim = (450,450) resized_img = cv2.resize (img, new_dim, interpolation=cv2.INTER_AREA) cv2.imshow ("Original Image", img) cv2.imshow ("Resized Image", resized_img) cv2.waitKey (0) cv2.destroyAllWindows
The following is the image shown from the code above.
This looks okay, but it's better to keep the aspect ratio of the output image. So I prefer to resize by keeping the aspect ratio.
The next step is to see how to create a watermark.
3. Create a watermark using an image
I chose to use the image of my name to add a watermark. Make an image of your name and try it with me.
To add a watermark to the center of the image:
If necessary, read and resize the image (watermark image, input image).
Import cv2img = cv2.imread ('images/deer.JPG') watermark = cv2.imread ("watermark.PNG") percent_of_scaling = 20new_width = int (img.shape [1] * percent_of_scaling/100) new_height = int (img.shape [0] * percent_of_scaling/100) new_dim = (new_width, new_height) resized_img = cv2.resize (img, new_dim) Interpolation=cv2.INTER_AREA) wm_scale = 40wm_width = int (watermark.shape [1] * wm_scale/100) wm_height = int (watermark.shape [0] * wm_scale/100) wm_dim = (wm_width, wm_height) resized_wm = cv2.resize (watermark, wm_dim, interpolation=cv2.INTER_AREA)
The position of the watermark is defined according to the new size of the resized input image.
H_img, w_img, _ = resized_img.shapecenter_y = int (h_img/2) center_x = int (w_img/2) h_wm, w_wm, _ = resized_wm.shapetop_y = center_y-int (h_wm/2) left_x = center_x-int (w_wm/2) bottom_y = top_y + h_wmright_x = left_x + w_wm
Get the * * rectangular region (ROI) * * of interest and store it in a variable named "roi".
Roi = resized_ IMG [top _ y:bottom_y, left_x:right_x]
Use * * cv2.addWeighted () * * to overlay the resized watermark on the ROI and store it in a variable named "result".
Result = cv2.addWeighted (roi, 1, resized_wm, 0.3,0)
Now, add this result to the resized input image
Resized_ IMG [top _ y:bottom_y, left_x:right_x] = result
Save the generated watermark image to your computer
Filename = 'watermarked_deer.jpg'cv2.imwrite (filename, resized_img)
Display the generated watermark image
Cv2.imshow ("Resized Input Image", resized_img) cv2.waitKey (0) cv2.destroyAllWindows ()
Here is the generated watermark image.
So far, we have learned to watermark a single image. Since our goal is to watermark multiple images, we need to create a list of all these input images and loop through it.
Here is the image that will be used.
Create a list of input images
Import osfolderPath = "images" imgList = os.listdir (folderPath) imgList
Code that watermarks multiple images
Import cv2watermark = cv2.imread ("watermark.PNG") wm_scale = 40wm_width = int (watermark.shape [1] * wm_scale/100) wm_height = int (watermark.shape [0] * wm_scale/100) wm_dim = (wm_width, wm_height) resized_wm = cv2.resize (watermark, wm_dim, interpolation=cv2.INTER_AREA) h_wm, w_wm _ = resized_wm.shape for image in imgList: img = cv2.imread (f'{folderPath} / {image}') percent_of_scaling = 20 new_width = int (img.shape [1] * percent_of_scaling/100) new_height = int (img.shape [0] * percent_of_scaling/100) new_dim = (new_width, new_height) resized_img = cv2.resize (img, new_dim, interpolation=cv2.INTER_AREA) h_img W_img, _ = resized_img.shape center_y = int (h_img/2) center_x = int (w_img/2) top_y = center_y-int (h_wm/2) left_x = center_x-int (w_wm/2) bottom_y = top_y + h_wm right_x = left_x + w_wm roi = resized_ IMG [top _ y:bottom_y Left_x:right_x] result = cv2.addWeighted (roi, 1, resized_wm, 0.3,0) resized_ IMG [top _ y:bottom_y, left_x:right_x] = result filename = os.path.basename (image) cv2.imwrite ("watermarked images/watermarked_" + filename, resized_img) cv2.imshow ("Watermarked Image", resized_img) cv2.waitKey (0) cv2.destroyAllWindows ()
Finally, we complete the task of watermarking all input images.
The above is all the contents of the article "how to use OpenCV to watermark an image". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.