top of page

WATERMARKING IMAGES USING OPENCV

Writer's picture: madrasresearchorgmadrasresearchorg

Author : Divyangi Singh

Watermark is a logo or a signature superimposed on a background image plenty of transparency. It is majorly used to protect the copyrights of pictures. Using watermarks will prevent anyone (even your competitors) from making use of your images. If you implement them, it will help you protect your work and discourage anyone who wants to use your image to promote their project or business from doing so without your permission.
 

OPENCV LIBRARY:

Opensource Computer Vision Library abbreviated as OpenCV is a open source machine learning software library. The library contains 2500+ optimized algorithms which are used to identify objects, pictures, faces etc. It can process images and videos to identify objects, faces or even the handwriting of a human. When it is integrated with various libraries, such as numpy which is a highly optimized library for numerical operations, a lot of complex problems in machine learning can be solved.

ADDING AN IMAGE AS A WATERMARK:

Before starting with the project, it is important that you have a logo image with black background as OpenCV as it is easier to crop the black background from the logo image, while imposing it on the background image , simply by changing the RGB values.

CODE:

Import the important libraries.

  • All the important library for our project is imported.

import numpy as np 
import pandas as pd import cv2 
from google.colab import files 
from google.colab.patches import cv2_imshow
from skimage import io
from PIL import Image import matplotlib.pylab as plt
  • Upload the logo image and the background image

Upload the logo image (preferably with black background and a background image). You can view the images simply by reading it using cv2.imread() function and displaying it using cv2.imshow().

uploaded= files.upload()
  • Define the pre-processing function which is used to set the position of logo on the background image

A pre-processing function is displayed which helps in setting the position of the logo on the background image. You can check the coordinates using the matplotlib library functions as well.


def preprocessing(src, overlay, pos=(0, 0), scale=1): 
    overlay = cv2.resize(overlay, (0, 0), fx=scale, fy=scale)
    h, w, _ = overlay.shape  
    rows, cols, _ = src.shape  
    y, x = pos[0], pos[0] 
    for i in range(h):
     for j in range(w):
         if x + i >= rows or y + j >= cols:
             continue
         alpha = float(overlay[i][j][0] / 255.0) 
         src[x + i][y + j] = alpha * overlay[i][j][:3] + (1 - alpha)*src[x + i][y + j]
    return src

  • Define the function to transparently overlay the logo image on the background image

A function is defined which imposes the logo image on the background image according to the provided coordinates and scale of opacity.


def transparentOverlay(backgroundImage, overlayImage, pos=(0, 0), scale=1):
   overlayImage = cv2.resize(overlayImage, (0, 0), fx=scale, fy=scale)
    h, w, _ = overlayImage.shape  # Size of foreground
    rows, cols, _ = backgroundImage.shape  # Size of background Image
    y, x = pos[0], pos[1]  # Position of foreground/overlayImage image

    # loop over all pixels and apply the blending equation
    for i in range(h):
        for j in range(w):
           if x + i >= rows or y + j >= cols:
                continue
           alpha = float(overlayImage[i][j][2] / 255.0)  # read the alpha channel
           backgroundImage[x + i][y + j] = alpha * overlayImage[i][j][:3] + (1 - alpha) * backgroundImage[x + i][y + j]
    return backgroundImage

  • Define the function which adds the watermark to the background image

A function is declared which returns the watermarked image. It creates a copy of the main and logo image and doesn’t not change the original images.

def addImageWatermark(LogoImage,MainImage,opacity,pos=(100,10),): 
opacity = opacity / 100 
OriImg = cv2.imread(MainImage, -1) 
waterImg = cv2.imread(LogoImage, -1) 
tempImg = OriImg.copy() 
print(tempImg.shape) overlay = transparentOverlay(tempImg, waterImg, pos) output = OriImg.copy() # apply the overlay cv2.addWeighted(overlay, opacity, output, 1 - opacity, 0, output) cv2_imshow(output)
  • Call the add Watermark function

The addImageWatermark function declared above is called in the main() function and the uploaded images along with desired opacity level and coordinates for logo is provided as parameters.

if __name__ == '__main__': 
addImageWatermark('logop.png','nature.jpg',50,(100,10))

OUTPUT:

FIG:1

CONCLUSION:

In this blog, it has been illustrated how we can easily add watermarks on images using OpenCV collaborated with different libraries like numpy, matplotlib etc.

GITHUB LINK:

  • https://github.com/Zizi32/ImageWatermarkingusingOpenCV

REFERENCES:

  • https://analyticsindiamag.com/how-to-create-a-watermark-on-images-using-opencv/

  • https://www.c-sharpcorner.com/article/blending-and-watermarking-images-in-python-using-opencv/

  • https://morioh.com/p/036ab83f7f62



Recent Posts

See All

Comments


Madras Scientific  Research Foundation

About US

 

Contact

 

Blog

 

Internship

 

Join us 

Know How In Action 

bottom of page