Image Watermarking Tool in Python.
About the project: This is a project for an Image Watermarking Tool in Python. This will be a self-contained script that uses the powerful Pillow library to add a semi-transparent text watermark to an image.
- Before You Run the Code
- Before you can use this script, you'll need to install the Pillow library. Open your terminal or command prompt and run the following command:
pip install Pillow
- This project will prompt you for the path of the image you want to watermark, the text you want to use, and where to save the new file.
This project provides a complete and interactive Image Watermarking Tool. You can run it from your terminal and follow the prompts. The script is designed to be user-friendly and handles basic errors like a missing input file.
Project Level: Intermediate
You can directly copy the below snippet code with the help of green copy button, paste it and run it in any Python editor you have.
Steps: Follow these stepsStep 1: Copy below code using green 'copy' button.
Step 2: Paste the code on your chosen editor.
Step 3: Save the code with filename and .py extention.
Step 4: Run (Press F5 if using python IDLE)
# image_watermarking_tool.py
import os
from PIL import Image, ImageDraw, ImageFont
def add_watermark(input_image_path, output_image_path, watermark_text):
"""
Adds a semi-transparent text watermark to an image.
Args:
input_image_path (str): Path to the original image file.
output_image_path (str): Path to save the watermarked image.
watermark_text (str): The text to use for the watermark.
"""
try:
# Open the original image
img = Image.open(input_image_path).convert("RGBA")
# Get image dimensions
width, height = img.size
# Create a transparent background for the watermark text
transparent_layer = Image.new("RGBA", (width, height), (0, 0, 0, 0))
draw = ImageDraw.Draw(transparent_layer)
# Try to load a font, fall back to a default if not found
try:
# You can change 'arial.ttf' to another font file on your system
font = ImageFont.truetype("arial.ttf", int(height / 15))
except IOError:
print("Warning: Arial font not found. Using default font.")
font = ImageFont.load_default()
# Calculate text size and position to place it in the bottom-right corner
text_bbox = draw.textbbox((0, 0), watermark_text, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]
# Position the text with some padding
padding = 10
x = width - text_width - padding
y = height - text_height - padding
# Define the watermark color (light gray with 50% transparency)
# (Red, Green, Blue, Alpha)
watermark_color = (192, 192, 192, 128)
# Draw the watermark text onto the transparent layer
draw.text((x, y), watermark_text, font=font, fill=watermark_color)
# Combine the original image with the transparent watermark layer
watermarked_img = Image.alpha_composite(img, transparent_layer)
# Convert back to RGB for saving (JPEG doesn't support transparency)
watermarked_img = watermarked_img.convert("RGB")
# Save the watermarked image
watermarked_img.save(output_image_path)
print(f"Successfully watermarked image saved as: {output_image_path}")
except FileNotFoundError:
print(f"Error: The file '{input_image_path}' was not found.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
def main():
"""
Main function to run the image watermarking tool.
"""
print("--- Python Image Watermarking Tool ---")
# Get user inputs
input_image_path = input("Enter the path to the image you want to watermark: ").strip()
if not input_image_path:
print("Input path cannot be empty. Exiting.")
return
watermark_text = input("Enter the watermark text: ").strip()
if not watermark_text:
print("Watermark text cannot be empty. Exiting.")
return
# Default output path
output_image_path = input(f"Enter the output path (e.g., watermarked_image.jpg), or press Enter for default: ").strip()
if not output_image_path:
# Create a default output path based on the input filename
base, ext = os.path.splitext(input_image_path)
output_image_path = f"{base}_watermarked.jpg"
add_watermark(input_image_path, output_image_path, watermark_text)
# This ensures that main() is called only when the script is executed directly.
if __name__ == "__main__":
main()
← Back to Projects