Fundamentals 6 min read

How to Add and Remove Image Watermarks Using Python (PIL & OpenCV)

This tutorial demonstrates several Python techniques—including PIL and OpenCV code—for adding text, transparent image, batch, rotated watermarks, and even removing watermarks from pictures, providing practical examples for image processing in the digital media era.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
How to Add and Remove Image Watermarks Using Python (PIL & OpenCV)

In the digital media era, image watermarking is essential for copyright protection, brand identification, and content authentication, and Python offers simple ways to add or remove watermarks.

Example 1: Adding a text watermark with the PIL library

from PIL import Image, ImageDraw, ImageFont

def add_text_watermark(image_path, output_path, text, position):
    image = Image.open(image_path)
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype("arial.ttf", 36)
    draw.text(position, text, font=font, fill=(255, 255, 255))
    image.save(output_path)

add_text_watermark('original.jpg', 'watermarked.jpg', 'My Watermark', (10, 10))

Example 2: Adding a semi‑transparent image watermark with PIL

from PIL import Image

def add_image_watermark(base_image_path, watermark_image_path, output_path):
    base_image = Image.open(base_image_path)
    watermark = Image.open(watermark_image_path).convert("RGBA")
    # Set watermark opacity
    alpha = 0.3
    watermark.putalpha(int(255 * alpha))
    # Position at bottom‑right corner
    position = (base_image.width - watermark.width, base_image.height - watermark.height)
    base_image.paste(watermark, position, watermark)
    base_image.save(output_path)

add_image_watermark('original.jpg', 'logo.png', 'watermarked_with_logo.jpg')

Example 3: Batch adding watermarks to a folder of images

from PIL import Image
import os

def batch_add_watermark(images_dir, watermark_path, output_dir):
    watermark = Image.open(watermark_path).convert("RGBA")
    alpha = 0.3
    watermark.putalpha(int(255 * alpha))
    for filename in os.listdir(images_dir):
        if filename.endswith('.jpg'):
            base_image = Image.open(os.path.join(images_dir, filename))
            position = (base_image.width - watermark.width, base_image.height - watermark.height)
            base_image.paste(watermark, position, watermark)
            base_image.save(os.path.join(output_dir, filename))

batch_add_watermark('images', 'logo.png', 'output')

Example 4: Adding a rotated watermark using OpenCV

import cv2
import numpy as np

def add_rotated_watermark(image_path, watermark_path, output_path):
    img = cv2.imread(image_path)
    watermark = cv2.imread(watermark_path, cv2.IMREAD_UNCHANGED)
    # Resize watermark
    scale_percent = 10  # percent of original size
    width = int(watermark.shape[1] * scale_percent / 100)
    height = int(watermark.shape[0] * scale_percent / 100)
    dim = (width, height)
    watermark = cv2.resize(watermark, dim, interpolation=cv2.INTER_AREA)
    # Rotate watermark
    rows, cols, _ = watermark.shape
    M = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 1)
    watermark = cv2.warpAffine(watermark, M, (cols, rows))
    # Blend watermark onto image
    for c in range(0, 3):
        img[:rows, :cols, c] = watermark[:,:,c]*watermark[:,:,3]/255 + img[:rows, :cols, c]*(1-watermark[:,:,3]/255)
    cv2.imwrite(output_path, img)

add_rotated_watermark('original.jpg', 'logo.png', 'watermarked_rotated.jpg')

Example 5: Simple watermark removal using blur (advanced)

# Removing a watermark is complex; this example uses a basic blur on the watermark area.
import cv2
import numpy as np

def remove_watermark(image_path, watermark_area, output_path):
    img = cv2.imread(image_path)
    # Blur the specified region
    img[watermark_area[1]:watermark_area[1]+watermark_area[3], watermark_area[0]:watermark_area[0]+watermark_area[2]] = \
        cv2.blur(img[watermark_area[1]:watermark_area[1]+watermark_area[3], watermark_area[0]:watermark_area[0]+watermark_area[2]], (10, 10))
    cv2.imwrite(output_path, img)

remove_watermark('watermarked.jpg', [10, 10, 200, 200], 'cleaned.jpg')

By following these examples, you can master basic techniques for adding and removing watermarks with Python, which are useful for protecting copyrights or enhancing visual projects, and you are encouraged to explore further tutorials on image processing, machine learning, and data science.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

OpenCVimage-processing
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.