top of page

Python Automation for Graphic Design: Batch Resize Images and Add Watermark


Blog image post with a fun illustration showing small image files behind a computer with python code moving to larger image files with a watermark.
Blog Image Post with Small Image Files moving to larger image files with watermark.

As a graphic designer and Python developer who loves to blend creativity with efficiency, I often find myself exploring ways to streamline repetitive tasks in my design workflow. One such task involves resizing a batch of images and adding a watermark – a task that can be time-consuming when done manually. Fortunately, Python, my trusted companion, comes to the rescue with its versatile libraries. In this blog post, I want to share one way that I use Python as a powerful and innovative tool in my world of graphic design.


In my daily work, Python has become a personal assistant, simplifying and speeding up numerous repetitive tasks that I encounter in graphic design. Whether it's resizing, cropping, or renaming a multitude of image files, Python scripts have become my trusted allies for automating these processes. I've delved into Python libraries like Pillow and OpenCV, utilizing their capabilities to process images in bulk, effectively streamlining my workflow.


To illustrate this, let's take a closer look at how Python, in conjunction with the Pillow library, can effortlessly resize images and add watermarks, saving you both time and effort in your graphic design projects.


First, make sure you have the Pillow library installed. You can install it using pip:

pip install pillow

Now, you can use the following Python code to automate the image resizing and watermarking task:

from PIL import Image, ImageDraw, ImageFont
import os

# Define the directory where your images are located
input_directory = "input_images/"

# Define the directory where the processed images will be saved
output_directory = "output_images/"

# Define the watermark text and font
watermark_text = "The Southern Tech Lady"
font = ImageFont.truetype("arial.ttf", 36)

# Define the size to which you want to resize the images
new_size = (800, 600)

# Create the output directory if it doesn't exist
if not os.path.exists(output_directory):
    os.makedirs(output_directory)

# List all image files in the input directory
image_files = [f for f in os.listdir(input_directory) if f.endswith((".jpg", ".jpeg", ".png"))]

for image_file in image_files:
    # Open the image
    image = Image.open(os.path.join(input_directory, image_file))

    # Resize the image
    image = image.resize(new_size)

    # Create a drawing context for adding a watermark
    draw = ImageDraw.Draw(image)

    # Calculate the position to place the watermark (e.g., bottom right corner)
    text_width, text_height = draw.textsize(watermark_text, font)
    image_width, image_height = image.size
    margin = 10
    position = (image_width - text_width - margin, image_height - text_height - margin)

    # Add the watermark
    draw.text(position, watermark_text, fill=(255, 255, 255), font=font)

    # Save the processed image to the output directory
    output_file = os.path.join(output_directory,       f"processed_{image_file}")
    image.save(output_file)

    print(f"Processed: {image_file}")

print("All images processed.")

In this example, we define the input and output directories, specify the watermark text, font, and the desired image size. Next, the code processes each image in the input directory, resizes it, adds the watermark, and saves the final image in the output directory.


Make sure to adjust the input and output directories, watermark text, font, and image size to fit your specific needs.


By utilizing Python for tasks like these, we graphic designers can embrace automation, leaving more room for our creative energies to flow.





Comments


bottom of page