Create your own image watermark using OpenCV-Python

Photos with watermarks are certainly something you see more and more of these days online. You might be wondering: What’s the point of putting watermarks on your images? Should I add them to my content? Moreover, why? Watermarks can be used to legal documents to verify their legitimacy and preserve confidential information

When it comes to branding, you can see watermarks on a lot of studio quality images, designs, works of art, and even memes, especially on social media. Influencers and content producers watermark their work so that, in the event that something they produce ends up going viral, their brand will be recognized. 

What is a Watermark?

A watermark is a picture, overlay, or text placed on top of a digital file.

In this tutorial, we’ll have fun learning how to use OpenCV to apply a watermark to an image.

So, let’s get this tutorial started…

In this lesson, we will want an image and a png file of your watermark with a black backdrop, as it is simpler to crop the black background from the png image. Then we import them after making sure that your system has all the necessary Python libraries installed.

#import necessary modules
import cv2
import os
import glob

Following the import of all required modules, we will first read the watermark png file and its dimensions.

# here we will take the watermark png file & its dimensions.
watermark = cv2.imread('WATERMARK.png')
watermark_h, watermark_w, _ = watermark.shape
# here we will take the image png file and its dimensions.
images_path = glob.glob('IMAGE.jpg')

Make sure that the dimensions of the original image and the watermark image are the same after loading both. If not, adjust the image size as desired. Set the logo’s position to match the resized input image’s new dimensions. Obtain the ROI and store it in the ‘roi’ variable.

for img_path in images_path:
    img = cv2.imread(img_path)
    img_h, img_w, _ = img.shape

    center_img_h = int(img_h / 2)
    center_img_w = int(img_w / 2)

    top_y = center_img_h - int(watermark_h / 2)
    left_x = center_img_w - int(watermark_w / 2)

    bottom_y = top_y + watermark_h
    right_x = left_x + watermark_w

    #, (left_x, top_y), 10, (0, 255, 0), -1)
    #, (right_x, bottom_y), 10, (0, 255, 0), -1)

    roi = img[top_y: bottom_y, left_x: right_x]
    #Using cv2. addWeighted(), overlay the resized watermark onto the ROI , and then put it right in the variable "result."

    result = cv2.addWeighted(roi, 1, watermark, 0.3, 0)
    img[top_y: bottom_y, left_x: right_x] = result

    filename = os.path.basename(img_path)

    cv2.imwrite('watermarked_' + filename, img)
#The cv2.imshow() function is used to draw the picture on the window
cv2.imshow('Watermark', watermark)
cv2.imshow('Img', img)
cv2.imshow('Roi', roi)
cv2.imshow('Result', result)

Hurray, it’s all finished! Let’s have a look at the results.

In this tutorial, we learned how to use OpenCV to add our own watermarks to the images. I hope you’ve enjoyed reading this article.


Leave a Reply

Your email address will not be published.