Expanding the Canvas Without Resizing in Python Pillow
Last Updated :
08 Jul, 2024
The Python Imaging Library (PIL) is a powerful tool for image processing in Python, offering a wide range of functionalities from basic image manipulations to complex operations. One of the useful features of PIL is the ability to expand the canvas of an image without resizing the original content. This article explores the concept of expanding the canvas without resizing the image content in PIL 1.1.6 and provides three practical examples demonstrating this feature.
Expanding the Canvas Without Resizing in Python Image Library (PIL) 1.1.6
Expanding the canvas of an image means increasing the dimensions of the image while keeping the original content unchanged. The additional space added around the image can be filled with a solid color, a pattern, or made transparent. This process involves creating a new image with the desired dimensions and pasting the original image onto it at a specified position.
The primary function used for this task in PIL is Image.new(), which creates a new image, and Image.paste(), which pastes the original image onto the new canvas.
Examples of Expanding the Canvas Without Resizing
Here are three examples demonstrating how to expand the canvas of an image using PIL 1.1.6. Each example will include code and a description of the output.
Real Image:
Adding a Solid Color Border
In this example, we will add a solid color border around an image.
Python
from PIL import Image
import matplotlib.pyplot as plt
# Open the original image
original_image = Image.open("gfg.png")
# Define the new canvas size
border_size = 50
new_width = original_image.width + 2 * border_size
new_height = original_image.height + 2 * border_size
# Create a new image with a solid color (white) background
new_image = Image.new("RGB", (new_width, new_height), (255, 255, 255))
# Paste the original image onto the center of the new image
new_image.paste(original_image, (border_size, border_size))
# Display the result using matplotlib
plt.imshow(new_image)
plt.axis('off') # Hide axes
plt.show()
# Save the result
new_image.save("example.png")
Output
The resulting image will have the original content centered with a 50-pixel white border around it.
Adding Padding for Text Annotations
In this example, we will expand the canvas to add space below the image for text annotations.
Python
from PIL import Image
import matplotlib.pyplot as plt
# Open the original image
original_image = Image.open("gfg.png")
# Define the new canvas size
padding_size = 100
new_width = original_image.width
new_height = original_image.height + padding_size
# Create a new image with a solid color (white) background
new_image = Image.new("RGB", (new_width, new_height), (255, 255, 255))
# Paste the original image onto the top of the new image
new_image.paste(original_image, (0, 0))
# Display the result using matplotlib
plt.imshow(new_image)
plt.axis('off') # Hide axes
plt.show()
# Save the result
new_image.save("example.png")
Output
The resulting image will have the original content on top with a 100-pixel white space below for adding annotations.
Creating a Collage Background
In this example, we will expand the canvas to create a background for a collage of multiple images.
Python
from PIL import Image
import matplotlib.pyplot as plt
# Open the original images
image1 = Image.open("gfg.png")
image2 = Image.open("gfg.png")
# Define the new canvas size (twice the width and height of one image)
new_width = image1.width * 2
new_height = image1.height * 2
# Create a new image with a solid color (white) background
new_image = Image.new("RGB", (new_width, new_height), (255, 255, 255))
# Paste the original images onto the new canvas
new_image.paste(image1, (0, 0))
new_image.paste(image2, (image1.width, 0))
# Display the result using matplotlib
plt.imshow(new_image)
plt.axis('off') # Hide axes
plt.show()
# Save the result
new_image.save("example.png")
Output
The resulting image will be a 2x2 grid where the first image is placed in the top-left corner and the second image in the top-right corner, with space available for more images or content.
Conclusion
Expanding the canvas of an image without resizing the original content is a straightforward yet powerful technique in image processing. Using the Python Imaging Library (PIL) 1.1.6, you can easily achieve this by creating a new image with the desired dimensions and pasting the original image onto it. The examples provided in this article illustrate different scenarios where expanding the canvas can be useful, such as adding borders, padding for annotations, or creating backgrounds for collages.
Similar Reads
Python | Working with the Image Data Type in pillow In this article, we will look into some attributes of an Image object that will give information about the image and the file it was loaded from. For this, we will need to import image module from pillow. Image we will be working on : size() method - It helps to get the dimensions of an image. IMG =
2 min read
Change image resolution using Pillow in Python Prerequisites: Python pillow PIL is the Python Imaging Library which provides the python interpreter with an in-depth file format support, an efficient internal representation, and fairly powerful image processing capabilities. Changing the resolution of an image simply means reducing or increasing
2 min read
How to change figure size in Plotly in Python In this article, we will discuss how we can change the figure size in Plotly in Python. Let's firstly take about Plotly in Python. Plotly  Python library provides us with an interactive open-source Plotly library that can support over 40 unique chart types that cover a wide list of statistical, fin
4 min read
How to resize Image in Python - Tkinter? Python provides multiple options for building GUI (Graphical User Interface) applications. Among them, Tkinter is one of the most widely used and simplest options. It comes bundled with Python and serves as a standard interface to the Tk GUI toolkit.However, Tkinter alone does not provide support fo
2 min read
Matplotlib.figure.Figure.set_canvas() in Python Matplotlib is a library in Python and it is numerical â mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot
2 min read