PyTorch Resize Images

In PyTorch, Resize() function is used to resize the input image to a specified size. The torchvision.transforms module gives various image transforms. In detail, we will discuss Resizing images using PyTorch in Python.

And additionally, we will also cover different examples related to PyTorch resize images. And we will cover these topics.

  • PyTorch resize image
  • PyTorch resize image example
  • How PyTorch resize image tensor
  • How PyTorch resize image transform
  • PyTorch resize 3d image
  • How PyTorch resize an image on GPU
  • PyTorch resize image batch
  • How PyTorch resize input image

PyTorch resize image

In this section, we will learn about the PyTorch resize an image by using Resize() function in python.

The Resize() function is used to alter resizes the input image to a specified size. This transform gives various transformations by the torchvision.transforms module.

Syntax:

Syntax of PyTorch resize image:

torchvision.transforms.Resize(size)

Parameter:

The following is the parameter of PyTorch resize image:

Size: Size is a parameter that the input image is to be resized. size is a series like(h,w) where h is the height and w is the weight of the output images in the batch. If the size of the image is in int format then the size of the image will be a square image after resizing.

So, with this, we understood about PyTorch resize an image using Resize() function.

Read How to use PyTorch Polar

PyTorch resize image example

In this section, we will learn how to implement the PyTorch resize image with the help of an example in python.

The torchvision.transforms module gives various image transforms. There is a Resize() function that is used to resize the input image to a specified size. The Resize() function accepts both PIL and tensor images.

Code:

In the following code we will import all the necessary libraries such as import torch, import torchvision.transforms as T, import Image from PIL, and import matplotlib.pyplot as plot.

  • imge = Image.open(‘Butterfly.png’): Here we are reading the input image.
  • size = imge.size is used to compute the size of the image.
  • transforms = T.Resize(size = (200,400)) is used to define transform or resize the image with the given size.
  • imge = transforms(imge) is used to apply the transformation on the input image.
  • print(“The size after resize:”, imge.size) is used to print the size after resize by using print() function.
# Importing Libraries
import torch
import torchvision.transforms as T
from PIL import Image
import matplotlib.pyplot as plot

# Read the input image
imge = Image.open('NewYork.png')

# Compute the size(width, height) of image
size = imge.size
print("The size of the original image:", size)

# Define transform or resize the image with given size
transforms = T.Resize(size = (200,400))

# Apply the transformation on the input image
imge = transforms(imge)
print("The size after resize:", imge.size)
plot.imshow(imge)
plot.show()

Output:

After running the above code, we get the following output in which we can see that the size of the image after resizing is printed on the screen.

PyTorch resize image example

This is how we understood the implementation of the resize image with the help od an example.

Read How to use PyTorch Cat function

How PyTorch resize image tensor

In this section, we will learn about the PyTorch resize image tensor in python.

The image tensor is defined as a torch tensor with shape (C,H,W). Here C represents the number of channels, H represents the image height and the W represents the image width.

The Resize() function is used to resize the input image tensor to a specified size.

Code:

In the following code we will import all the necessary libraries such as import torch, import torchvision.transforms as T, import Image from PIL, import matplotlib.pyplot as plot.

  • image = Image.open(‘Flower.jpg’) Here we read the PIL image.
  • size = image.size is used to compute the size of the image.
  • print(“The size of the original image:”, size) is used to print the size of the original image.
  • transforms = trans.Resize(size = (300,500)) is used to define transform or resize the image tensor.
  • image = transforms(image) is used to apply the transformation on the input image.
  • print(“The size after resize:”, image.size) is used to print the size after resize.
# import the required libraries
import torch
import torchvision.transforms as trans
from PIL import Image
import matplotlib.pyplot as plot

# Read the input image
image = Image.open('Chicago.jpg')

# compute the size of the image
size = image.size
print("The size of the original image:", size)

# Define transform or resize the image tensor
transforms = trans.Resize(size = (300,500))

# Apply the transformation on the input image
image = transforms(image)
print("The size after resize:", image.size)
plot.imshow(image)
plot.show()

Output:

After running the above code, we get the following output in which we can see that the size of the tensor image is resized and printed on the screen.

PyTorch resize image tensor
PyTorch resize image tensor

So, with this, we understood the PyTorch resize image tensor.

Read PyTorch Stack Tutorial

How PyTorch resize image transform

In this section, we will learn about the PyTorch resize image transform in python.

The PyTorch resize image transforms are used to resize the input image to the given size. If the image is of a torch tensor then it has H, W shape.

Syntax:

Syntax of PyTorch resize image transform:

torchvision.transforms.Resize(size, interpollation=InterpolationMode.BILINEAR, max_size=None, antialias=None)

Parameters:

  • size: size is defined as the desired output size. The size is a series like(h,w) where h is the height and w is the weight of the output images in the batch. If the size of the image is in int format then the size of the image will be a square image after resizing.
  • interpolation: The desired interpolation is defined as an enum defined by torchvision.transforms.InterploationMode.If the input is tensor only InterpolationMode.Nearest are supported.
  • max_size: The maximum size allowed for the larger edge of the resized image. If the larger edge of the image is greater than max_size after being resized according to the size.
  • antialias: If the image is the PIL image the flag is ignored and antialias are always used. If the image is tensor the flag is by default False and can be set to True for InterpolationMode.BILINEAR.

So, with this, we understood the PyTorch resize image transform by using torchvision.transform.Reshape().

Read Create PyTorch Empty Tensor

PyTorch resize 3d image

In this section, we will learn about the PyTorch resize 3d image in python.

The PyTorch resize 3d image is used to resize the 3D image to the given size. The Resize() function accepts both PIL and tensor images.

Code:

In the following code, we will import all the necessary libraries such as import torch, import matplotlib.pyplot as plot, import numpy as np, import transforms from torchvision, import Image from PIL.

  • image = Image.open(‘teddy.jpg’) is used to read a PIL image.
  • size = image.size is used to compute the size of the image.
  • transforms = transforms.Resize(size = (350,550)) is used to define transform or resize the 3D image.
  • image = transforms(image) is used of the transformation on the input image.
# Importing Libraries
import torch  
import matplotlib.pyplot as plt  
import numpy as np  
from torchvision import transforms  
from PIL import Image 

# Read a PIL Image
image = Image.open('Houston.jpg')
image
# Compute the size(width, height) of image
size = image.size
print(size)


# Define transform or resize the 3D image 
transforms = transforms.Resize(size = (350,550))

# Use of the transformation on the input image
image = transforms(image)
print("The size after resize:", image.size)
plot.imshow(image)
plot.show()

Output:

After running the above code, we get the following output in which we can see that the PyTorch 3D image resize values is printed on the screen.

PyTorch resize 3d image
PyTorch resize 3d image

This is how we understand PyTorch resize a 3D image in python.

Read How to use PyTorch Full() Function

How PyTorch resize an image on GPU

In this section, we will learn about the PyTorch resizing an image on GPU in python.

Before moving forward we should have a piece of knowledge about the GPU.

GPU stands for Graphical Processing Unit and is a trained electronic circuit that is designed to handle and change memories to increase the design of images.

Code:

In the following code, we will import all the necessary libraries such as import torch, import matplotlib.pyplot as plot, import numpy as np, import transforms from torchvision, import Image from PIL.

  • image = Image.open(‘Rose.jpg’) is used to read a PIL Image.
  • device = torch.device(“cuda” if torch.cuda.is_available() else “CPU”) is used to define path and set GPU.
  • size = image.size is used to compute the size of the image.
  • transforms = transforms.Resize(size = (450,650)) is used to Define transform or resize the an image on GPU.
  • image = transforms(image) is use of the transformation on the input image.
  • print(“The size after resize:”, image.size) is used to print the size after resize.
# Importing Libraries
import torch  
import matplotlib.pyplot as plot  
import numpy as np  
from torchvision import transforms  
from PIL import Image 

# Read a PIL Image
image = Image.open('San Diego.jpg')
image

# Define path and set GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Compute the size(width, height) of image
size = image.size
print(size)


# Define transform or resize the an image on GPU
transforms = transforms.Resize(size = (450,650))

# Use of the transformation on the input image
image = transforms(image)
print("The size after resize:", image.size)
plot.imshow(image)
plot.show()

Output:

In the below output, you can see that PyTorch resizes an image on GPU printed on the screen.

PyTorch resize image on GPU
PyTorch resize image on GPU

So, with this, we understood about the PyTorch resize image on GPU with the help of Resize() function.

PyTorch resize image batch

In this section, we will learn about the PyTorch resize image batch in python.

Before moving forward we should have a piece of knowledge about the batch.

A batch is defined as a process arrange things in sets or groups. Here we resize the image in batch by using the Resize() function.

Code:

In the following code, firstly we will import all the necessary libraries such as import torch, import torchvision.transforms.functional, import Import from PIL.

  • image = Image.open(“Philadelphia.jpg”): Here we are reading the image.
  • print(“Size of actual image:”,image.size, “\n”) is used to print the size of the original image with the help of print() function.
  • cropimage = fn.center_crop(image, output_size=[450]) is used to crop the image by using fn.center_crop().
  • resizeimage = fn.resize(image, size=[200]) is used to resize the image.
  • print(“Size of resized image:”,resizeimage.size,”\n”) is used to print the size of the resized image.
# Import Library
import torch
import torchvision.transforms.functional as fn
from PIL import Image

# Read the image
image = Image.open("Philadelphia.jpg")
print("Size of actual image:",image.size, "\n")
image

# Crop the image
cropimage = fn.center_crop(image, output_size=[450])
print("Size of cropped image:",cropimage.size,"\n")
cropimage

# Resize the image
resizeimage = fn.resize(image, size=[200])
print("Size of resized image:",resizeimage.size,"\n")
resizeimage

Output:

After running the above code we get the following output in which we can see that the Size of the actual image, cropped image, and resize image are printed on the screen.

PyTorch resize image batch
PyTorch resize image batch

This is how we understand PyTorch resizing an image in the form of the batch by using Resize() function.

Read PyTorch Flatten

How PyTorch resize input image

In this section, we will learn about the PyTorch resize input image in python.

In PyTorch there is a Resize() function that is used to alter resizes the input image to a specified size. This transformation gives various transformations by the torchvision.transforms module.

Code:

In the following code, we will import all the necessary libraries such as import torch, import requests, import torchvision.transforms, import Image from PIL.

  • image = Image.open(“Philadelphia.jpg”) is used to load the image.
  • print(“Size of actual image:”,image.size, “\n”) is used to print the size of the actual image.
  • preprocess = T.Compose([ T.Resize(246), T.CenterCrop(234) ]) is used as resized the image.
  • print(“Size of resized image” ,y.size,”\n”) is used to print the size of the resized image by using print() function.
# Importing Libraries
import torch
import requests
import torchvision.transforms as T
from PIL import Image

# Read the image
image = Image.open("Philadelphia.jpg")
print("Size of actual image:",image.size, "\n")
image

# Resize the image
preprocess = T.Compose([
   T.Resize(246),
   T.CenterCrop(234)
])

y = preprocess(image)
print("Size of resized image" ,y.size,"\n")
y

Output:

After running the above code, we get the following output in which we can see that the PyTorch resize input image is printed on the screen.

PyTorch resize input image
PyTorch resize input image

So, with this, we understood about how to resize the input image with the help of Resize() function in Pytorch.

So, in this tutorial, we discussed PyTorch resize image and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • PyTorch resize image
  • PyTorch resize image example
  • How to PyTorch resize image tensor
  • How PyTorch resize image transform
  • PyTorch resize 3d image
  • How PyTorch resize an image on GPU
  • PyTorch resize image batch
  • How PyTorch resize input image

You may like the following PyTorch tutorials: