Last modified: Apr 21, 2025 By Alexander Williams
Python Simple Image Border Addition Guide
Adding borders to images is a common task in image processing. Python makes it easy with libraries like PIL. This guide shows you how.
Table Of Contents
Why Add Borders to Images?
Borders improve image presentation. They create separation from backgrounds. Borders also prepare images for printing or display.
Common uses include photo frames, thumbnails, and social media posts. Borders can highlight important content too.
Required Libraries
You need Python installed. The main library is Pillow (PIL). Install it using pip:
pip install Pillow
Basic Border Addition
The ImageOps.expand()
method adds borders. It takes the image, border width, and color as inputs.
from PIL import Image, ImageOps
# Open image
img = Image.open("input.jpg")
# Add 50px white border
bordered_img = ImageOps.expand(img, border=50, fill="white")
# Save result
bordered_img.save("output.jpg")
Border Color Options
You can use any color for borders. Specify RGB values or color names. Here are examples:
# Red border using name
ImageOps.expand(img, border=30, fill="red")
# Custom RGB color (purple)
ImageOps.expand(img, border=40, fill=(128, 0, 128))
Different Borders per Side
For asymmetric borders, use a tuple. The order is (top, right, bottom, left).
# Different borders on each side
borders = (20, 40, 60, 80) # top, right, bottom, left
ImageOps.expand(img, border=borders, fill="blue")
Border with Padding
Combine borders with padding for better results. First resize the image, then add border.
from PIL import Image, ImageOps
# Resize image first
img = img.resize((400, 300))
# Then add border
bordered = ImageOps.expand(img, border=20, fill="black")
Advanced Border Effects
Create fancy borders by combining techniques. Use multiple borders or gradients. For more advanced effects, check our Python Image Collages Guide.
Common Issues
Watch for these problems when adding borders:
- Image distortion from incorrect sizing
- Color mismatches between border and image
- Memory errors with very large images
For sizing help, see our Python Image Dimensions Guide.
Performance Tips
Large images take more processing time. Consider these optimizations:
- Process smaller versions first
- Use efficient color formats
- Batch process multiple images
Example With Output
Here's a complete example with before/after:
from PIL import Image, ImageOps
# Original image (200x200)
original = Image.new("RGB", (200, 200), "yellow")
# Add 50px green border
bordered = ImageOps.expand(original, border=50, fill="green")
# Save both
original.save("original.jpg")
bordered.save("bordered.jpg")
# Output: Two images created
# original.jpg (200x200 yellow square)
# bordered.jpg (300x300 with green border)
Conclusion
Adding borders in Python is simple with PIL. The ImageOps.expand()
method handles most cases. For more image processing, see our Python Image Rescaling Guide.
Experiment with different border styles and colors. Combine with other techniques for professional results.