Batch resize and watermark images using Pillow. The watermark should be a transparent PNG centered on the image using alpha composition, scaling proportionally to always be 20% of the target image width.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Here's example Python code using Pillow that batch resizes images to fit within 800x800 pixels, scales the watermark to 20% of the image width, centers it with alpha composition, and saves the output:
python
from PIL import Image
import os
input_dir = 'input'
output_dir = 'output'
os.makedirs(output_dir, exist_ok=True)
watermark = Image.open('watermark.png').convert('RGBA')
for filename in os.listdir(input_dir):
if not filename.lower().endswith(('.png', '.jpg', '.jpeg')):
continue
image_path = os.path.join(input_dir, filename)
image = Image.open(image_path).convert('RGBA')
# Resize base image maintaining aspect ratio
image.thumbnail((800, 800), Image.LANCZOS)
# Scale watermark to 20% of image width
wm_scale = (image.width * 0.2) / watermark.width
wm_size = (int(watermark.width * wm_scale), int(watermark.height * wm_scale))
wm_resized = watermark.resize(wm_size, Image.LANCZOS)
# Calculate position to center watermark
position = ((image.width - wm_resized.width) // 2, (image.height - wm_resized.height) // 2)
# Create transparent layer and paste watermark
transparent = Image.new('RGBA', image.size)
transparent.paste(wm_resized, position, wm_resized)
# Composite the watermark with the image
watermarked = Image.alpha_composite(image, transparent)
# Convert to RGB and save as JPEG; use PNG if keeping transparency
output_path = os.path.join(output_dir, filename)
watermarked.convert('RGB').save(output_path, 'JPEG')
This script processes all images in the input folder, resizes and watermarks them, and saves to output. Adjust dimensions and formats as needed.
You can do this in one pass with Pillow by opening the base image and the watermark as RGBA, resizing both as needed, and then compositing them. A simple batch script might look like this:
python
from pathlib import Path
from PIL import Image
input_dir = Path("input_images")
output_dir = Path("output_images")
watermark_path = Path("watermark.png")
# load watermark once
watermark = Image.open(watermark_path).convert("RGBA")
# make sure output exists
output_dir.mkdir(parents=True, exist_ok=True)
# maximum size to resize your input images to, adjust as needed
max_size = (1200, 1200)
for img_path in input_dir.iterdir():
if img_path.suffix.lower() not in {".jpg", ".jpeg", ".png"}:
continue
with Image.open(img_path).convert("RGBA") as base:
# resize base image to fit within max_size
base.thumbnail(max_size, Image.LANCZOS)
# scale watermark to 20% of the base image width, keeping aspect ratio
target_w = int(base.width * 0.20)
scale = target_w / watermark.width
wm_size = (target_w, int(watermark.height * scale))
wm_resized = watermark.resize(wm_size, Image.LANCZOS)
# compute centered position
pos = ((base.width - wm_resized.width) // 2,
(base.height - wm_resized.height) // 2)
# create a temporary layer for the watermark the same size as the base
layer = Image.new("RGBA", base.size, (0, 0, 0, 0))
layer.paste(wm_resized, pos, wm_resized) # use itself as mask
# alpha-composite the watermark onto the base image
composited = Image.alpha_composite(base, layer)
# save result; use PNG to preserve transparency or convert to RGB for JPEG
out_path = output_dir / img_path.name
if out_path.suffix.lower() == ".jpg":
composited.convert("RGB").save(out_path, quality=90)
else:
composited.save(out_path)
This script walks the input_images directory, resizes each image to a maximum size, scales the watermark to 20 % of the current image width, centers it, and saves the composited result to output_images. Adjust max_size, input/output paths, and output format to suit your needs.