How to Embed and Extract Blind Watermarks in Images with Python
This guide explains the concept of blind watermarking for image copyright protection, shows how to install the blind-watermark Python package, and provides step‑by‑step code examples for embedding and extracting both text and image watermarks while preserving the original visual appearance.
Overview
Images are frequently stolen, altered, or used without permission. Traditional visible watermarks (text or logos) are easy to see and remove, while blind watermarks embed invisible data directly into the image’s frequency domain, remaining robust against common attacks such as rotation, cropping, scaling, or noise addition, and they do not require the original image for extraction.
Installation
pip install blind-watermarkOr install the latest development version:
git clone [email protected]:guofei9987/blind_watermark.git
cd blind_watermark
pip install .Basic Usage
1. Embed a Text Watermark
Assume you have an original image ori_img.jpg and want to embed the string "开源技术小栈" as a watermark.
from blind_watermark import WaterMark
# Initialize the watermark object with image and watermark passwords (numeric keys)
wm = WaterMark(password_img=1, password_wm=1)
# Load the original image
wm.read_img('ori_img.jpg')
# Load the watermark string
wm.read_wm('开源技术小栈', mode='str')
# Embed the watermark and save the result
wm.embed('embedded.png')
print('Watermark bit length:', len(wm.wm_bit)) # prints the length of the binary watermarkAfter running, embedded.png looks identical to the original image, but the watermark is silently embedded.
2. Extract the Text Watermark
Extraction does not require the original image; only the watermark’s bit length and the passwords are needed.
wm_extract = WaterMark(password_img=1, password_wm=1)
wm_extract.extract('embedded.png', wm_shape=len(wm.wm_bit), mode='str')
print('Extracted watermark:', wm_extract.wm) # outputs: 开源技术小栈3. Embed an Image Watermark
If you want to use a small icon (e.g., a 128×128 PNG) as the watermark:
wm = WaterMark(password_img=1, password_wm=1)
wm.read_img('ori_img.jpg')
wm.read_wm('watermark_icon.png') # the image is automatically converted to binary bits
wm.embed('embedded_with_icon.png')To extract the image watermark:
wm_extract = WaterMark(password_img=1, password_wm=1)
wm_extract.extract('embedded_with_icon.png', wm_shape=(128, 128), out_wm_name='extracted_icon.png', mode='img')
# The file extracted_icon.png contains the recovered watermark imageOpen Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
