5 Best Ways to Create a Watermark on an Image Using OpenCV Python

πŸ’‘ Problem Formulation: Watermarking images can be essential for copyright protection or branding purposes. This article will walk through how to add a watermark to an image using Python’s OpenCV library, starting with a base image (e.g., a landscape photo) and rendering a transparent text or logo overlay as the desired output.

Method 1: Overlaying Transparent Text

Applying transparent text as a watermark requires blending the text with the original image while controlling opacity. OpenCV allows you to add text over an image and adjust transparency with the addWeighted() function, providing an easy way to create a simple text watermark.

Here’s an example:

import cv2
import numpy as np

# Load image
img = cv2.imread('image.jpg')

# Specify the font and draw the text using cv2.putText()
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img, 'Watermark', (10,500), font, 4, (255,255,255), 2, cv2.LINE_AA)

# Save the result
cv2.imwrite('watermarked_image.jpg', img)

Output: An image named ‘watermarked_image.jpg’ with ‘Watermark’ text at the bottom.

This code loads an image, adds semi-transparent white text ‘Watermark’ at the bottom using OpenCV’s putText() function, and saves the watermarked image. The (10,500) tuple specifies the position, font is the font type, 4 is the font scale, (255,255,255) is the color (white), and 2 is the thickness.

Method 2: Adding a Logo as a Watermark

To add a logo as a watermark, you must consider the channels of images: greyscale, color, and alpha for transparency. Using OpenCV, you can overlay an image with a transparent PNG logo by combining the images based on their alpha values to retain the transparency.

Here’s an example:

import cv2
import numpy as np

# Load images
img = cv2.imread('image.jpg')
logo = cv2.imread('logo.png', -1)

# Create ROI the same size as the logo
rows,cols,channels = logo.shape
roi = img[0:rows, 0:cols]

# Create a mask of the logo and invert it
logo_gray = cv2.cvtColor(logo, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(logo_gray, 10, 255, cv2.THRESH_BINARY)
mask_inv = cv2.bitwise_not(mask)

# Black-out the area of the logo in ROI
img_bg = cv2.bitwise_and(roi, roi, mask = mask_inv)

# Take only the logo
logo_fg = cv2.bitwise_and(logo, logo, mask = mask)

# Put the logo in the ROI and modify the main image
dst = cv2.add(img_bg, logo_fg)
img[0:rows, 0:cols] = dst

# Save the result
cv2.imwrite('logo_watermarked_image.jpg', img)

Output: An image named ‘logo_watermarked_image.jpg’ with the logo as a watermark at the top-left corner.

This snippet demonstrates how to place a logo on top of another image. It creates two masks to isolate the logo and the background of the region of interest, combines them and then places the composite on the original image. The masks ensure the logo’s transparency is respected.

Method 3: Full Image Opacity Watermarking

Another approach is to create a watermark that covers the full image with reduced opacity. This method involves adjusting the alpha channels to make the watermark superimposed over the entire image less opaque, giving it a subtle watermark effect.

Here’s an example:

import cv2
import numpy as np

# Load image and watermark
img = cv2.imread('image.jpg')
watermark = cv2.imread('watermark.jpg')
watermark = cv2.resize(watermark, (img.shape[1], img.shape[0]))

# Combine images
result = cv2.addWeighted(watermark, 0.5, img, 0.5, 0)

# Save result
cv2.imwrite('full_image_watermark.jpg', result)

Output: An image named ‘full_image_watermark.jpg’ with a watermark covering the whole image.

The code above creates a full-image watermark by resizing the watermark to match the original image and applying the addWeighted() function to blend them together. An alpha value of 0.5 gives equal weight to both images, thus producing a 50% opacity watermark.

Method 4: Watermark with Adjustable Opacity

For more control over the transparency of the watermark, you can use a weighted addition where the alpha value for the watermark’s opacity can be fine-tuned. This method grants more flexibility over how prominent the watermark appears on the image.

Here’s an example:

import cv2
import numpy as np

# Load image and watermark, ensure watermark is a PNG with transparency
img = cv2.imread('image.jpg')
watermark = cv2.imread('watermark.png', -1)
watermark = cv2.resize(watermark, (img.shape[1], img.shape[0]))

# Create an alpha mask
b,g,r,a = cv2.split(watermark)
overlay_color = cv2.merge((b,g,r))

# Use the alpha channel as a mask for opacity
mask = cv2.medianBlur(a, 5)

# Apply the watermark with the mask
img = cv2.add(img, cv2.bitwise_and(overlay_color, overlay_color, mask=mask))

# Save result
cv2.imwrite('adjustable_opacity_watermark.jpg', img)

Output: An image named ‘adjustable_opacity_watermark.jpg’ with a watermark that has adjustable opacity.

This code loads both the base image and the PNG watermark with an alpha channel. It creates a color overlay from the watermark and a mask based on its alpha channel. The watermark is then applied using the alpha mask to control the transparency.

Bonus One-Liner Method 5: Simplified Text Watermark

A one-liner method for adding a text watermark can be accomplished using Python’s OpenCV. It’s a quick, direct approach but lacks the customization of the previous methods.

Here’s an example:

cv2.imwrite('one_liner_watermark.jpg', cv2.putText(cv2.imread('image.jpg'), 'Watermark', (50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,255,255), 2, cv2.LINE_AA))

Output: An image named ‘one_liner_watermark.jpg’ with ‘Watermark’ text at the given position.

This one-liner is a condensed form of Method 1, where it reads an image, adds white text ‘Watermark’ at position (50,50), and writes it to a file all in a single line of code. It’s great for a quick watermark but doesn’t allow for granular opacity control.

Summary/Discussion

  • Method 1: Overlaying Transparent Text. Simple and quick for text watermarks. Limited flexibility with styling and transparency.
  • Method 2: Adding a Logo as a Watermark. Good for branding with logo images. Requires handling of transparency and is slightly more complex.
  • Method 3: Full Image Opacity Watermarking. Covers the entire image for a consistent look. Can be overpowering if not adjusted properly.
  • Method 4: Watermark with Adjustable Opacity. Offers precise control over watermark opacity. More complex and may require fine-tuning.
  • Bonus Method 5: Simplified Text Watermark. Fastest method for text watermarks. Not suitable for watermarks requiring transparency or creative placement.