Converting an image to a Torch Tensor in Python
Last Updated :
16 Jun, 2024
In this article, we will see how to convert an image to a PyTorch Tensor. A tensor in PyTorch is like a NumPy array containing elements of the same dtypes.
A tensor may be of scalar type, one-dimensional or multi-dimensional. To convert an image to a tensor in PyTorch we use PILToTensor() and ToTensor() transforms. These transforms are provided in the torchvision.transforms package. Using these transforms we can convert a PIL image or a numpy.ndarray. The numpy.ndarray must be in [H, W, C] format, where H, W, and C are the height, width, and a number of channels of the image.
transform = transforms.Compose([transforms.PILToTensor()])
tensor = transform(img)
This transform converts a PIL image to a tensor of data type torch.uint8 in the range between 0 and 255. Here img is a PIL image.
transform = transforms.Compose([transforms.ToTensor()])
tensor = transform(img)
This transform converts any numpy.ndarray to torch tensor of data type torch.float32 in range 0 and 1. Here img is a numpy.ndarray.
Approach:
- Import the required libraries.
- Read the input image. The input image is either PIL image or a NumPy N-dimensional array.
- Define the transform to convert the image to Torch Tensor. We define a transform using transforms.Compose(). You can directly use transforms.PILToTensor() or transforms.ToTensor().
- Convert the image to tensor using the above-defined transform.
- Print the tensor values.
The below image is used as an input image in both examples:
Example 1:
In the below example, we convert a PIL image to Torch Tensor.
Python
# Import necessary libraries
import torch
from PIL import Image
import torchvision.transforms as transforms
# Read a PIL image
image = Image.open('iceland.jpg')
# Define a transform to convert PIL
# image to a Torch tensor
transform = transforms.Compose([
transforms.PILToTensor()
])
# transform = transforms.PILToTensor()
# Convert the PIL image to Torch tensor
img_tensor = transform(image)
# print the converted Torch tensor
print(img_tensor)
Output:

Notice that the data type of the output tensor is torch.uint8 and the values are in range [0,255].
Example 2:
In this example, we read an RGB image using OpenCV. The type of image read using OpenCV is numpy.ndarray. We convert it to a torch tensor using the transform ToTensor().
Python
# Import required libraries
import torch
import cv2
import torchvision.transforms as transforms
# Read the image
image = cv2.imread('iceland.jpg')
# Convert BGR image to RGB image
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Define a transform to convert
# the image to torch tensor
transform = transforms.Compose([
transforms.ToTensor()
])
# Convert the image to Torch tensor
tensor = transform(image)
# print the converted image tensor
print(tensor)
Output:

Notice that the data type of the output tensor is torch.float32 and the values are in the range [0, 1].
Similar Reads
Converting a Pandas DataFrame to a PyTorch Tensor PyTorch is a powerful deep learning framework widely used for building and training neural networks. One of the essential steps in using PyTorch is converting data from various formats into tensors, which are the fundamental data structures used by PyTorch. Pandas DataFrames are a common data struct
5 min read
Converting a List of Tensors to a Single Tensor in PyTorch PyTorch, a popular deep learning framework, provides powerful tools for tensor manipulation. One common task in PyTorch is converting a list of tensors into a single tensor. This operation is crucial for various applications, including data preprocessing, model input preparation, and tensor operatio
4 min read
How to convert an image to grayscale in PyTorch In this article, we are going to see how to convert an image to grayscale in PyTorch. torchvision.transforms.grayscale method Grayscaling is the process of converting an image from other color spaces e.g. RGB, CMYK, HSV, etc. to shades of gray. It varies between complete black and complete white. t
2 min read
How To Convert Numpy Array To Tensor? We are given a NumPy array, and our task is to convert it into a TensorFlow tensor. This is useful when integrating NumPy-based data with TensorFlow pipelines, which support acceleration using GPU and TPU. For example, converting [[1, 2], [3, 4]] results in a TensorFlow object that looks like: Pytho
2 min read
How to convert torch tensor to pandas dataframe? When working with deep learning models in PyTorch, you often deal with tensors. However, there are situations where you may need to convert these tensors into a Pandas DataFrame, especially when you're preparing data for analysis or visualization. In this article, we'll explore how to convert a PyTo
5 min read