Watermarking images with OpenCV and Python

Watermarking images with OpenCV and Python

Watermarking images using OpenCV in Python involves overlaying a watermark (text or another image) onto an original image. This can be done by manipulating the pixel values. Here's a basic guide on how to watermark images with both text and another image using OpenCV.

1. Installing OpenCV

If you haven't installed OpenCV yet, you can do it using pip:

pip install opencv-python 

2. Watermarking with Text

To add a text watermark, you'll use the cv2.putText() function.

import cv2 # Load the image image = cv2.imread('path_to_your_image.jpg') # Specify watermark text and properties text = 'Watermark' position = (50, 50) # position of the text font = cv2.FONT_HERSHEY_SIMPLEX # font font_scale = 1 # font scale font_color = (255, 255, 255) # font color (B, G, R) font_thickness = 2 # font thickness # Apply the text cv2.putText(image, text, position, font, font_scale, font_color, font_thickness) # Save or display the image cv2.imwrite('watermarked_image.jpg', image) # cv2.imshow('Watermarked Image', image) # cv2.waitKey(0) # cv2.destroyAllWindows() 

3. Watermarking with Another Image

To overlay an image watermark, you need to adjust the alpha values to blend the watermark with the original image.

import cv2 import numpy as np # Load the original image and the watermark image original_image = cv2.imread('path_to_your_image.jpg') watermark_image = cv2.imread('path_to_watermark_image.png', cv2.IMREAD_UNCHANGED) # Resize watermark to fit on the original image # This step is optional and depends on your watermark image and needs scaled_watermark = cv2.resize(watermark_image, (100, 100)) # example size # Position of the watermark x_offset = 50 y_offset = 50 x_end = x_offset + scaled_watermark.shape[1] y_end = y_offset + scaled_watermark.shape[0] # Extract the alpha channel from the watermark image (if it exists) if scaled_watermark.shape[2] == 4: alpha_s = scaled_watermark[:, :, 3] / 255.0 alpha_l = 1.0 - alpha_s # Overlay the watermark for c in range(0, 3): original_image[y_offset:y_end, x_offset:x_end, c] = \ (alpha_s * scaled_watermark[:, :, c] + \ alpha_l * original_image[y_offset:y_end, x_offset:x_end, c]) # Save or display the image cv2.imwrite('watermarked_image.jpg', original_image) # cv2.imshow('Watermarked Image', original_image) # cv2.waitKey(0) # cv2.destroyAllWindows() 

In this example, it's assumed that the watermark image has an alpha channel for transparency. The watermark image is blended with the original image based on the alpha values.

Notes

  • For text watermarking, you can adjust the font, size, color, and position as needed.
  • For image watermarking, ensure the watermark's size is appropriate for the original image. You might also need to handle the alpha channel for transparency.
  • Always ensure you have the legal right to modify and watermark the images you're working with.

More Tags

hash arraybuffer iccube entity-framework-core editorfor spring-repositories vue-loader contextmanager host mat-tab

More Programming Guides

Other Guides

More Programming Examples