Advanced Image Processing with Pillow
Pillow offers advanced image processing techniques such as masking, blending, creating thumbnails, and using alpha transparency to create dynamic and visually appealing images.
Masking and Blending Images
Masking Images
Masking allows you to combine images based on a mask, where the mask controls the transparency of the overlay.
from PIL import Image
# Open base image and overlay image
base_img = Image.open("background.jpg")
overlay_img = Image.open("overlay.png").resize(base_img.size)
# Create a mask (grayscale)
mask = Image.open("mask.png").convert("L")
# Combine images using the mask
blended_img = Image.composite(overlay_img, base_img, mask)
blended_img.show()
Blending Images
Blending combines two images with a specified alpha (transparency) value.
# Blend images with 50% transparency
blended_img = Image.blend(base_img, overlay_img, alpha=0.5)
blended_img.show()
Creating Thumbnails
Thumbnails are smaller representations of images, useful for previews or galleries.
Example: Create a Thumbnail
# Open an image
img = Image.open("example.jpg")
# Create a thumbnail with a max size of (128, 128)
img.thumbnail((128, 128))
img.show()
Thumbnails preserve the aspect ratio of the image while resizing it.
Using Alpha Transparency
Alpha transparency enables the creation of images with transparent regions.
Adding an Alpha Channel
# Open an image
img = Image.open("example.jpg").convert("RGBA")
# Create a transparent region
alpha = Image.new("L", img.size, 128) # 50% transparency
img.putalpha(alpha)
img.show()
Working with Transparent Overlays
# Open base and overlay images
base_img = Image.open("background.jpg").convert("RGBA")
overlay_img = Image.open("overlay.png").convert("RGBA").resize(base_img.size)
# Blend using alpha transparency
blended_img = Image.alpha_composite(base_img, overlay_img)
blended_img.show()
Practical Applications
Example 1: Watermarking an Image
Add a transparent text watermark to an image.
from PIL import ImageDraw, ImageFont
# Open an image
img = Image.open("photo.jpg").convert("RGBA")
# Create a watermark
watermark = Image.new("RGBA", img.size, (255, 255, 255, 0))
draw = ImageDraw.Draw(watermark)
font = ImageFont.truetype("arial.ttf", 36)
draw.text((10, 10), "Watermark", fill=(255, 255, 255, 128), font=font)
# Apply the watermark
watermarked_img = Image.alpha_composite(img, watermark)
watermarked_img.show()
Example 2: Create a Circular Thumbnail
Crop an image into a circular thumbnail using masking.
# Open an image
img = Image.open("photo.jpg").resize((200, 200))
# Create a circular mask
mask = Image.new("L", img.size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0, img.size[0], img.size[1]), fill=255)
# Apply the mask
circular_thumb = Image.composite(img, Image.new("RGB", img.size, "white"), mask)
circular_thumb.show()
Try It Yourself
Problem 1: Blend Two Images
Write a script to blend two images (image1.jpg
and image2.jpg
) with 70% transparency for image1
.
Show Solution
from PIL import Image
img1 = Image.open("image1.jpg")
img2 = Image.open("image2.jpg").resize(img1.size)
blended_img = Image.blend(img1, img2, alpha=0.7)
blended_img.show()
Problem 2: Create a Thumbnail Gallery
Create thumbnails for all .jpg
images in a folder and save them in a thumbnails
directory.
Show Solution
import os
from PIL import Image
input_folder = "images"
output_folder = "thumbnails"
os.makedirs(output_folder, exist_ok=True)
for filename in os.listdir(input_folder):
if filename.endswith(".jpg"):
img = Image.open(os.path.join(input_folder, filename))
img.thumbnail((128, 128))
img.save(os.path.join(output_folder, filename))
Master these advanced techniques to create visually stunning images and add professional touches to your projects with Pillow!