The Palos Publishing Company

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

Create a watermarking tool

Here’s a simple Python script to create a watermarking tool that adds a semi-transparent text watermark to images. It uses the Pillow library for image processing:

python
from PIL import Image, ImageDraw, ImageFont def add_watermark(input_image_path, output_image_path, watermark_text, position=(0, 0), opacity=128, font_size=36): # Open the original image original = Image.open(input_image_path).convert("RGBA") # Make a blank image for the watermark with transparent background watermark = Image.new("RGBA", original.size, (255,255,255,0)) # Get a drawing context draw = ImageDraw.Draw(watermark) # Load a font try: font = ImageFont.truetype("arial.ttf", font_size) except IOError: font = ImageFont.load_default() # Calculate the width and height of the text to be added text_width, text_height = draw.textsize(watermark_text, font) # Set position if set to 'center' if position == 'center': position = ((original.width - text_width) // 2, (original.height - text_height) // 2) # Draw the text on the watermark image draw.text(position, watermark_text, fill=(255, 255, 255, opacity), font=font) # Combine original image with watermark watermarked = Image.alpha_composite(original, watermark) # Convert back to RGB and save the result watermarked.convert("RGB").save(output_image_path, "JPEG") # Example usage add_watermark("input.jpg", "output.jpg", "Sample Watermark", position='center', opacity=100, font_size=48)

This script will add a semi-transparent white watermark text centered on your image. You can customize text, position, opacity, and font size. Just replace "input.jpg" and "output.jpg" with your file paths.

Would you like a version for batch watermarking multiple images or with image watermark support?

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