Posted in

How to add a border to an image using Pillow?

Adding a border to an image can significantly enhance its visual appeal, making it stand out in various contexts, from personal photo editing to professional graphic design. As a dedicated Pillow supplier, I’ve witnessed firsthand how versatile and powerful the Pillow library in Python can be for such tasks. In this blog, I’ll guide you through the process of adding a border to an image using Pillow, sharing tips and tricks along the way to help you achieve the best results. Pillow

Understanding Pillow

Pillow is a fork of the Python Imaging Library (PIL) and is widely used for opening, manipulating, and saving many different image file formats. It provides a simple yet powerful API for performing a wide range of image processing tasks, including resizing, cropping, and adding borders. One of the key advantages of using Pillow is its cross – platform compatibility, which means you can use it on Windows, macOS, and Linux systems without any major issues.

Installation

Before you can start adding borders to images, you need to have Pillow installed. If you haven’t installed it yet, you can use pip, the Python package installer. Open your terminal or command prompt and run the following command:

pip install pillow

This command will download and install the latest version of Pillow from the Python Package Index (PyPI).

Basic Steps to Add a Border

Let’s start with the basic steps of adding a border to an image using Pillow. First, you need to import the necessary modules. In Python, the Image module from Pillow is the main entry – point for working with images.

from PIL import Image

# Open the image file
image = Image.open('your_image.jpg')

# Define the border width and color
border_width = 20
border_color = 'black'

# Create a new image with the border
new_image = Image.new(image.mode, (image.width + 2 * border_width, image.height + 2 * border_width), border_color)

# Paste the original image onto the new image
new_image.paste(image, (border_width, border_width))

# Save the new image
new_image.save('new_image_with_border.jpg')

In this code snippet, we first open an existing image using the Image.open() method. Then, we define the width and color of the border. We create a new, larger image with the specified background color, and finally, we paste the original image onto the new image at the appropriate position. The result is saved as a new file.

Customizing the Border

The basic example above gives you a simple, solid – colored border. However, you can customize the border in many ways to achieve different effects.

Changing the Border Color

You’re not limited to using basic colors like black or white. Pillow supports a wide range of color formats, including RGB tuples. For example, if you want a bright red border, you can use the following code:

from PIL import Image

image = Image.open('your_image.jpg')
border_width = 20
border_color = (255, 0, 0)  # RGB for red

new_image = Image.new(image.mode, (image.width + 2 * border_width, image.height + 2 * border_width), border_color)
new_image.paste(image, (border_width, border_width))
new_image.save('red_border_image.jpg')

Creating a Gradient Border

Creating a gradient border is a bit more complex but can add a very professional and eye – catching look to your image. You can achieve this by creating a gradient image and then slicing and pasting it around the original image.

from PIL import Image, ImageDraw

# Open the image
image = Image.open('your_image.jpg')
border_width = 20

# Create a gradient image
gradient = Image.new('RGB', (border_width, image.height), color=(0, 0, 0))
draw = ImageDraw.Draw(gradient)
for y in range(image.height):
    r, g, b = int((y / image.height) * 255), int((y / image.height) * 255), int((y / image.height) * 255)
    draw.line((0, y, border_width, y), fill=(r, g, b))

# Create a new image with the border
new_image = Image.new(image.mode, (image.width + 2 * border_width, image.height + 2 * border_width), (0, 0, 0))
new_image.paste(image, (border_width, border_width))

# Paste the gradient on the left and right sides
new_image.paste(gradient, (0, border_width))
flipped_gradient = gradient.transpose(Image.FLIP_LEFT_RIGHT)
new_image.paste(flipped_gradient, (image.width + border_width, border_width))

# Save the image
new_image.save('gradient_border_image.jpg')

In this example, we first create a vertical gradient image. Then we create a new image with space for the border and paste the original image in the middle. Finally, we paste the gradient image on the left and right sides of the original image, after flipping it for the right – hand side.

Advanced Border Effects

Beyond simple colors and gradients, you can also create more advanced border effects, such as a rounded – corner border.

from PIL import Image, ImageDraw, ImageOps

# Open the image
image = Image.open('your_image.jpg')
border_width = 20
radius = 30

# Create a mask for rounded corners
mask = Image.new('L', (image.width + 2 * border_width, image.height + 2 * border_width), 0)
draw = ImageDraw.Draw(mask)
draw.rounded_rectangle((0, 0, mask.width, mask.height), radius=radius, fill=255)

# Create a new image with the border
border_color = (255, 255, 255)
new_image = Image.new(image.mode, (image.width + 2 * border_width, image.height + 2 * border_width), border_color)
new_image.paste(image, (border_width, border_width))

# Apply the mask
new_image = Image.composite(new_image, Image.new(image.mode, new_image.size, (0, 0, 0)), mask)

# Save the image
new_image.save('rounded_border_image.jpg')

In this code, we first create a mask for the rounded corners using ImageDraw.rounded_rectangle(). Then we create a new image with the border and paste the original image onto it. Finally, we use Image.composite() to apply the mask, which results in an image with rounded – corner borders.

Use Cases

Adding borders to images has numerous practical use cases. In digital marketing, a well – designed border can make an image more prominent in social media posts or email newsletters. For photographers, borders can add a professional touch to their portfolio images. Graphic designers can use borders to create a cohesive look across different elements in a design project.

Why Choose Our Pillow Products

As a leading pillow supplier, we offer high – quality Pillow – related resources. Our libraries are well – maintained, with regular updates to ensure compatibility with the latest Python versions and security patches. We also provide comprehensive documentation and excellent customer support. Whether you’re a beginner just starting to explore image processing or an experienced developer looking for advanced features, our Pillow products can meet your needs.

Microfiber Pillow If you’re interested in using our Pillow – related resources for your image processing projects, or if you have any questions about adding borders or other image manipulation tasks, we’d love to hear from you. Contact us to start a procurement discussion and find out how we can help you achieve your goals.

References

  • Pillow official documentation
  • Python official documentation
  • Various online image processing tutorials

Hangzhou Aurora Textiles Co., Ltd.
As one of the most professional pillow manufacturers and suppliers in China, we also support custom service. Please feel free to wholesale bulk high-end pillow at competitive price from our factory. Good service and quality products are available.
Address: Room 1403, Building 1, Hangzhou Xiaoshan International Business Center, No. 458 Jincheng Road, Beigan Street, Xiaoshan District, Hangzhou City
E-mail: linda@aurorahometex.com
WebSite: https://www.aurorahometex.com/