Horje
Expanding the Canvas Without Resizing in Python Pillow

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:

gfg

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.

1

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.

2

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 2×2 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.

3

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.




Reffered: https://www.geeksforgeeks.org


Python

Related
How to Get Data from API in Python Flask How to Get Data from API in Python Flask
How to Concatenate Two Lists Index Wise in Python How to Concatenate Two Lists Index Wise in Python
Understanding Django: Model() vs Model.objects.create() Understanding Django: Model() vs Model.objects.create()
How to fix Python Multiple Inheritance generates "TypeError: got multiple values for keyword argument". How to fix Python Multiple Inheritance generates "TypeError: got multiple values for keyword argument".
Splitting Cells and Counting Unique Values in Python List Splitting Cells and Counting Unique Values in Python List

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
15