![]() |
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.6Expanding 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 ResizingHere 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 BorderIn this example, we will add a solid color border around an image.
Output The resulting image will have the original content centered with a 50-pixel white border around it. ![]() Adding Padding for Text AnnotationsIn this example, we will expand the canvas to add space below the image for text annotations.
Output The resulting image will have the original content on top with a 100-pixel white space below for adding annotations. ![]() Creating a Collage BackgroundIn this example, we will expand the canvas to create a background for a collage of multiple images.
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. ![]() ConclusionExpanding 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 |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 15 |