pytorch

How to Pad a Specific Image on All Sides in PyTorch?

Padding is a way of adding extra space around an image. In PyTorch, the “torchvision.transforms” module provides a “Pad()” transform/method to pad an image on all sides. Users can set/apply different paddings for the top, bottom, left, and right sides of the image. This method produces a new padded image of a specified size.

This article will demonstrate the method to pad a specific image on all sides in PyTorch.

How to Pad a Specific Image on All Sides in PyTorch?

To pad a specific image on all sides in PyTorch, check out the provided steps:

Step 1: Upload an Image to Google Colab

First, open Google Colab and click on the below-highlighted icons. Then, choose the specific image from the computer and upload it:


Upon doing so, the image will be uploaded to Google Colab:


Here, we have uploaded the following image and we will apply padding on its all sides:


Step 2: Import Necessary Library

Next, import the required libraries. For instance, we have imported the following libraries:

import torch
import torchvision.transforms as transforms
from PIL import Image

 
Here:

    • import torch” imports the PyTorch library.
    • import torchvision.transforms as transforms” imports the transforms module from torchvision that is used to preprocess image data before feeding it into a neural network.
    • from PIL import Image” is used to open and save different image file formats:


Step 3: Read the Input Image

After that, read the input image from the computer. Here, we are reading the “my_img.jpg” and storing it in the “input_img” variable:

input_img = Image.open('my_img.jpg')

 

Step 4: Compute Input Image’s Width and Height

Next, compute/get the width and height of the input image:

w, h = input_img.size

 

Step 5: Define a Transform

Now, define a transform to pad the input image on all sides. Here, we have specified padding side “50” that will apply the same padding to all sides of the image i.e., left, right, top, and bottom:

transform = transforms.Pad(50)

 

Step 6: Apply Transform on Image

Then, apply the above transform on the desired input image to apply padding on it:

padded_img = transform(input_img)

 

Step 7: Resize Image to the Initial Dimension

After that, resize the padded image to its actual dimension:

padded_img = padded_img.resize((w, h))

 

Step 8: Display the Padded Image

Finally, view the padded image by displaying it:

padded_img

 

The above output shows that the input image has been successfully padded on all sides.

Similarly, users can also specify other padding sizes in the “Pad()” method to pad the image with different padding sizes. Here, we are specifying left/right padding “20” and top/bottom padding “60”:

transform = transforms.Pad((20, 60))

 

This will apply 20 units of padding to the left and right sides of the image and 60 units of padding to the top and bottom of the image:


Furthermore, users can also specify different padding for the left, top, right, and bottom sides of the image:

transform = transforms.Pad((20, 40, 60, 80))

 

This will apply 20 units of padding to the left side, 40 units of padding on top, 60 units of padding on the right, and 80 units of padding on the bottom of the image:

Comparison

The comparison between original image and padded images with different sizes can be seen below:

Original Image

 

Padded Image (50)

 

Padded Image (20, 60)

Padded Image(20, 40, 60, 80)

Note: You can access our Google Colab Notebook at this link.

That was all about padding a specific image on all sides in PyTorch.

Conclusion

To pad a specific image on all sides in PyTorch, first, upload the desired image to Google Colab. Then, import the necessary libraries and read the input image. After that, conclude the width and height of the input image. Next, define a transform using the “Pad()” method and apply it to the desired input image to pad it on all sides. Lastly, resize the padded image to its original dimension and display it. This article has demonstrated the method to pad a specific image on all sides in PyTorch.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.