Introduction
Watermarking images is a practical approach for asserting copyright and guarding against unauthorized use. In Python, the Pillow library streamlines the process, enabling effortless addition of watermarks to your visuals with just a few lines of code. This tutorial guides you through basic to advanced methods of watermarking using Pillow.
Basic Watermarking
Let’s kick off with a straightforward example where we overlay a simple text watermark onto an image.
# Import the necessary libraries
from PIL import Image, ImageDraw, ImageFont
# Load an image
image = Image.open('your-image.jpg')
# Initialize the drawing context with the image as background
watermark_text = 'Sample Watermark'
text_position = (100, 100)
font = ImageFont.load_default()
draw = ImageDraw.Draw(image)
draw.text(text_position, watermark_text, font=font, fill=(255, 255, 255))
# Save the watermarked image
image.save('watermarked-image.jpg')
This snug segment of code adds a basic text watermark. Customize the `text_position` and `fill` arguments to alter the watermark’s position and color, respectively.
Custom Fonts and Opacity
Moving to a more sophisticated watermark, let’s use a custom font and add transparency to the text.
# Import the necessary libraries from Pillow
from PIL import Image, ImageDraw, ImageFont
# Load the image and font
image = Image.open('your-image.jpg')
font = ImageFont.truetype('arial.ttf', 36)
# Specify the text to draw, along with the position
watermark_text = 'Advanced Watermark'
text_position = (100, 100)
# Specify the transparency level for the watermark
watermark_transparency = 128 # Ranges from 0 to 255
# Prepare the watermark text
watermark = Image.new('RGBA', image.size, (255, 255, 255, 0))
draw = ImageDraw.Draw(watermark)
draw.text(text_position, watermark_text, font=font, fill=(255, 255, 255, watermark_transparency))
# Merge the watermark with the image
watermarked_image = Image.alpha_composite(image.convert('RGBA'), watermark)
# Save the image
watermarked_image.save('advanced-watermarked-image.png', 'PNG')
By creating a new temporary image with the same dimensions as the original and using it to compose the watermark, we achieve a semi-transparent effect. Note that saving the watermarked image in a format that supports transparency, such as PNG, is essential.
Watermarking with Images
Now let’s discuss how to overlay an image-based watermark such as a logo.
# Load the main image and the watermark image
image = Image.open('your-image.jpg')
watermark = Image.open('your-logo.png')
# Set the watermark size and position
watermark_size = (100, 100)
watermark_position = image.size[0] - watermark_size[0] * 2, image.size[1] - watermark_size[1] * 2
watermark = watermark.resize(watermark_size)
# Paste the watermark onto the image
transparent = Image.new('RGBA', image.size)
transparent.paste(image, (0, 0))
transparent.paste(watermark, watermark_position, mask=watermark)
transparent = transparent.convert('RGB')
# Save the result
transparent.save('image-watermarked.jpg')
In this example, we resize the watermark logo to an appropriate size and then paste it onto the original image. The positioning strategy should keep the watermark from obscuring the main content of the image.
Batch Processing
For those needing to watermark multiple images, iterating over a collection with a loop is key. Here’s how to watermark images in a directory in bulk:
# Import additional library
import os
# Specify the directory with your images
image_folder = 'path/to/your/images/'
# Process and watermark all images in the folder
for filename in os.listdir(image_folder):
if filename.endswith('.jpg'):
img = Image.open(os.path.join(image_folder, filename))
draw = ImageDraw.Draw(img)
draw.text(text_position, watermark_text, font=font, fill=(255, 255, 255, watermark_transparency))
img.save(os.path.join(image_folder, f'watermarked_{filename}'))
A bulk watermarking script can preserve the originality of images while consistently embedding your brand across various visuals.
Conclusion
Through this tutorial, we’ve learned how simple and flexible it is to add watermarks using Python and the Pillow library. Whether applying a text watermark, using custom fonts and transparency, overlaying image-based logos, or processing images in batch, Pillow equips you with the tools to protect and personalize your image content efficiently. Keep experimenting with positions, sizes, and transparency levels to fine-tune your results as needed.