The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Create a Python script for batch watermarking

Here’s a simple Python script to apply a watermark to multiple images in a batch. This uses the Pillow library for image manipulation and os to handle file system operations. The watermark will be placed in the bottom right corner of each image.

Prerequisites:

You need to install the Pillow library if you haven’t already:

bash
pip install pillow

Python script:

python
from PIL import Image, ImageDraw, ImageFont import os # Function to apply watermark to an image def apply_watermark(image_path, watermark_text, output_path): # Open the original image original_image = Image.open(image_path) width, height = original_image.size # Make the image editable watermark_image = original_image.copy() draw = ImageDraw.Draw(watermark_image) # Choose font and size font = ImageFont.load_default() text_width, text_height = draw.textsize(watermark_text, font) # Position of the watermark (bottom right corner) position = (width - text_width - 10, height - text_height - 10) # Apply the watermark text draw.text(position, watermark_text, font=font, fill=(255, 255, 255, 128)) # White color with transparency # Save the watermarked image watermark_image.save(output_path) # Function to batch watermark images in a directory def batch_watermark(input_folder, output_folder, watermark_text): # Check if output folder exists, if not create it if not os.path.exists(output_folder): os.makedirs(output_folder) # Loop through all files in the input folder for filename in os.listdir(input_folder): file_path = os.path.join(input_folder, filename) if os.path.isfile(file_path) and filename.lower().endswith(('jpg', 'jpeg', 'png')): output_path = os.path.join(output_folder, f"watermarked_{filename}") apply_watermark(file_path, watermark_text, output_path) print(f"Watermarked {filename} and saved to {output_path}") # Example usage input_folder = "path/to/your/images" output_folder = "path/to/save/watermarked/images" watermark_text = "Your Watermark" batch_watermark(input_folder, output_folder, watermark_text)

How it works:

  1. apply_watermark: This function applies a watermark text to a single image. It opens the image, creates a copy, and draws the watermark text in the bottom right corner with a slight transparency.

  2. batch_watermark: This function loops through all the images in the input directory, applies the watermark to each image, and saves the watermarked image in the output directory.

  3. The script handles jpg, jpeg, and png images by checking their extensions.

Notes:

  • The watermark text is drawn with a white color and some transparency (alpha value = 128). You can modify the fill parameter for different colors or transparency.

  • You can change the font size or style by using ImageFont.truetype() and providing a custom font file path.

Example Usage:

  1. Place your images in a folder, for example, images/.

  2. Define the input and output folder paths and the watermark text.

  3. Run the script to apply watermarks to all images in the input folder and save the results in the output folder.

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About