Life2Coding
How to Add Text Watermark on Images with OpenCV Python

This post will be helpful in learning OpenCV using Python programming. Here I will show how to implement OpenCV functions and apply them in various aspects using some great examples. Then the output will be visualized along with the comparisons.

We will also discuss the basic of image processing and provide the detail explanation related to the OpenCV functions.

Requirements:

First, you need to setup your Python Environment with OpenCV. You can easily do it by following Life2Coding’s tutorial on YouTube: Linking OpenCV with Python 3

Goals:

The goal is to make you understand how to draw text watermark on images using Python OpenCV.

Documentation:

imread()

retval=cv.imread(filename[, flags])

Loads an image from a file.

Parameters
filename Name of file to be loaded.
flags Flag that can take values of cv::ImreadModes

putText()

img=cv.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])

Draws a text string.

Parameters
img Image.
text Text string to be drawn.
org Bottom-left corner of the text string in the image.
fontFace Font type, see HersheyFonts.
fontScale Font scale factor that is multiplied by the font-specific base size.
color Text color.
thickness Thickness of the lines used to draw a text.
lineType Line type. See LineTypes
bottomLeftOrigin When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.

addWeighted()

dst=cv.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]])

Calculates the weighted sum of two arrays.

Parameters
src1 first input array.
alpha weight of the first array elements.
src2 second input array of the same size and channel number as src1.
beta weight of the second array elements.
gamma scalar added to each sum.
dst output array that has the same size and number of channels as the input arrays.
dtype optional depth of the output array; when both input arrays have the same depth, dtype can be set to -1, which will be equivalent to src1.depth().

imshow()

None=cv.imshow(winname, mat)

Displays an image in the specified window.

Parameters
winname Name of the window.
mat Image to be shown.

waitKey()

retval=cv.waitKey([, delay])

Waits for a pressed key.

Parameters
delay Delay in milliseconds. 0 is the special value that means “forever”.

Steps:

  • Load a face image using cv2.imread()
  • Load the image using cv2.imread()
  • Create the overlay copy of the image using copy()
  • Draw the watermark text using cv2.putText() on the overlay image
  • Then blend the images using cv2.addWeighted()
  • Display the image using cv2.imshow()
  • Wait for keyboard button press using cv2.waitKey()
  • Exit window and destroy all windows using cv2.destroyAllWindows()

Example Code:

# import the necessary packages
from __future__ import print_function
import numpy as np
import cv2

# load the image
image = cv2.imread("./hanif.jpg")

def addWaterMark(text,opacity,BGRColor):
    opacity=opacity/100
    overlay = image.copy()
    output = image.copy()

    cv2.putText(overlay, text, (0,int(2*(image.shape[1])/3)), cv2.FONT_HERSHEY_SIMPLEX, 1.0, BGRColor, 2)
    # apply the overlay
    cv2.addWeighted(overlay, opacity, output, 1 - opacity,
                    0, output)
    # show the output image
    cv2.imshow("Output", output)
    cv2.waitKey(0)

if __name__ == '__main__':
    #put the text , Opacity, BGR Color
    addWaterMark("Life2Coding",50,(255,0,255))

Output:

5cd124a82d047 How to Add Text Watermark on Images with OpenCV Python
life2coding_icon [] How to Add Text Watermark on Images with OpenCV Python
Latest posts by Life2Coding (see all)

One thought on “How to Add Text Watermark on Images with OpenCV Python

  1. ayse

    Hello,
    When i tried run the code, i have this error:
    overlay = image.copy()
    AttributeError: ‘NoneType’ object has no attribute ‘copy’ How can i fix this error?

    Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.