How to create a watermark on an image using OpenCV Python?

To add a watermark to an image, we use the cv2.addWeighted() function from OpenCV. This technique blends two images together by applying different opacity levels to create a semi-transparent watermark effect.

Step-by-Step Process

Here's the complete process to create a watermark on an image ?

Step 1: Import Required Libraries

Import OpenCV library for image processing ?

import cv2
import numpy as np

Step 2: Read Images

Load the main image and watermark image ?

# Read the main image
img = cv2.imread("main_image.jpg")

# Read the watermark image  
watermark = cv2.imread("watermark.png")

Step 3: Get Image Dimensions

Extract height and width of both images ?

# Get dimensions of main image
h_img, w_img = img.shape[:2]

# Get dimensions of watermark
h_wm, w_wm = watermark.shape[:2]

Step 4: Calculate Positioning

Determine where to place the watermark (center position) ?

# Calculate center coordinates
center_x = int(w_img/2)
center_y = int(h_img/2)

# Calculate region of interest (ROI) boundaries
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

Step 5: Apply Watermark

Blend the watermark with the selected region ?

# Extract region of interest from main image
roi = img[top_y:bottom_y, left_x:right_x]

# Blend images using addWeighted (roi: 100%, watermark: 30% opacity)
result = cv2.addWeighted(roi, 1, watermark, 0.3, 0)

# Replace the ROI with blended result
img[top_y:bottom_y, left_x:right_x] = result

Complete Example

Here's a complete working example that creates a watermark effect ?

import cv2
import numpy as np

# Create sample images for demonstration
def create_sample_images():
    # Create a blue main image
    main_img = np.ones((400, 600, 3), dtype=np.uint8) * 100
    cv2.rectangle(main_img, (50, 50), (550, 350), (255, 200, 100), -1)
    
    # Create a simple watermark
    watermark = np.ones((100, 200, 3), dtype=np.uint8) * 50
    cv2.putText(watermark, 'WATERMARK', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
    
    return main_img, watermark

# Create sample images
img, wm = create_sample_images()

# Get image dimensions
h_img, w_img = img.shape[:2]
h_wm, w_wm = wm.shape[:2]

# Calculate center position
center_x = int(w_img/2)
center_y = int(h_img/2)

# Calculate ROI coordinates
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

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

# Display result
cv2.imshow("Original with Watermark", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Understanding cv2.addWeighted() Parameters

The cv2.addWeighted() function syntax is ?

cv2.addWeighted(src1, alpha, src2, beta, gamma)
Parameter Description Example Value
src1 First input image (ROI) roi
alpha Weight for first image 1.0 (100%)
src2 Second input image (watermark) watermark
beta Weight for second image 0.3 (30%)
gamma Scalar added to result 0

Different Watermark Positions

You can position the watermark at different locations ?

# Top-left corner
top_y, left_x = 10, 10

# Top-right corner  
top_y, left_x = 10, w_img - w_wm - 10

# Bottom-left corner
top_y, left_x = h_img - h_wm - 10, 10

# Bottom-right corner
top_y, left_x = h_img - h_wm - 10, w_img - w_wm - 10

Conclusion

Use cv2.addWeighted() to create transparent watermarks by blending images. Adjust the beta parameter (0.1-0.5) to control watermark transparency. Position the watermark by calculating appropriate ROI coordinates based on image dimensions.

Updated on: 2026-03-26T22:07:32+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements