Conversion with PIL image, PyTorch tensor & NumPy array

Buy Me a Coffee☕

*Memos:

My post explains ToPILImage() about no arguments.

My post explains PILToTensor().

My post explains device conversion with to(), from_numpy() and numpy().

My post explains permute().

My post explains OxfordIIITPet().
You…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Buy Me a Coffee

*Memos:

from torchvision.datasets import OxfordIIITPet

origin_data = OxfordIIITPet(
    root="data",
    transform=None
)

import matplotlib.pyplot as plt

plt.figure(figsize=[7, 9])
plt.title(label="s500_394origin_data", fontsize=14)
plt.imshow(X=origin_data[0][0])
plt.show()

Image description

PIL image[H, W, C] => PyTorch tensor[C, H, W] => NumPy array[H, W, C] => PIL image[H, W, C]:

from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import PILToTensor, ToPILImage
import numpy as np

origin_data = OxfordIIITPet(
    root="data",
    transform=None
)

# PIL image to PyTorch tensor
ptt = PILToTensor()

pytorchimagetensor = ptt(origin_data[0][0])
# tensor([[[ 37,  35,  36,  ..., 247, 249, 249],
#          [ 35,  35,  37,  ..., 246, 248, 249],
#          ...,
#          [ 28,  28,  27,  ...,  59,  65,  76]],
#         [[ 20,  18,  19,  ..., 248, 248, 248],
#          [ 18,  18,  20,  ..., 247, 247, 248],
#          ...,
#          [ 27,  27,  27,  ...,  94, 106, 117]],
#         [[ 12,  10,  11,  ..., 253, 253, 253],
#          [ 10,  10,  12,  ..., 251, 252, 253],
#          ...,
#          [ 35,  35,  35,  ..., 214, 232, 223]]], dtype=torch.uint8)

# PyTorch tensor to NumPy array
numpyimagearray = pytorchimagetensor.permute(1, 2, 0).numpy()
numpyimagearray = np.array(object=pytorchimagetensor.permute(1, 2, 0))
numpyimagearray = np.asarray(pytorchimagetensor.permute(1, 2, 0))

numpyimagearray
# array([[[ 37  20  12]
#         [ 35  18  10]
#         ...
#         [249 248 253]]
#        [[ 35  18  10]
#         [ 35  18  10]
#         ...
#         [249 248 253]]
#        [[ 35  18  10]
#         [ 36  19  11]
#         ...
#         [250 249 254]]
#         ...
#        [[  5   6  24]
#         [  4   5  23]
#         ...
#         [ 69 110 224]]
#        [[  4   3  19]
#         [  3   2  18]
#         ...
#         [ 64 108 229]]
#        [[ 28  27  35]
#         [ 28  27  35]
#         ...
#         [ 76 117 223]]], dtype=uint8)

# NumPy array to PIL image
tpi = ToPILImage()
pilimage = tpi(numpyimagearray)

import matplotlib.pyplot as plt

plt.figure(figsize=[7, 9])
plt.title(label="s500_394origin_data", fontsize=14)
plt.imshow(X=pilimage)
plt.show()

Image description

PIL image[H, W, C] => NumPy array[H, W, C] => PyTorch tensor[C, H, W] => PIL image[H, W, C]:

from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import ToPILImage
import numpy as np
import torch

origin_data = OxfordIIITPet(
    root="data",
    transform=None
)

# PIL image to NumPy array
numpyimagearray = np.array(object=origin_data[0][0])
numpyimagearray = np.asarray(origin_data[0][0])

numpyimagearray
# array([[[ 37  20  12]
#         [ 35  18  10]
#         ...
#         [249 248 253]]
#        [[ 35  18  10]
#         [ 35  18  10]
#         ...
#         [249 248 253]]
#        [[ 35  18  10]
#         [ 36  19  11]
#         ...
#         [250 249 254]]
#         ...
#        [[  5   6  24]
#         [  4   5  23]
#         ...
#         [ 69 110 224]]
#        [[  4   3  19]
#         [  3   2  18]
#         ...
#         [ 64 108 229]]
#        [[ 28  27  35]
#         [ 28  27  35]
#         ...
#         [ 76 117 223]]], dtype=uint8)

# NumPy array to PyTorch tensor
pytorchimagetensor = torch.from_numpy(numpyimagearray).permute(dims=[2, 0, 1])
pytorchimagetensor = torch.tensor(numpyimagearray).permute(dims=[2, 0, 1])

pytorchimagetensor
# tensor([[[ 37,  35,  36,  ..., 247, 249, 249],
#          [ 35,  35,  37,  ..., 246, 248, 249],
#          ...,
#          [ 28,  28,  27,  ...,  59,  65,  76]],
#         [[ 20,  18,  19,  ..., 248, 248, 248],
#          [ 18,  18,  20,  ..., 247, 247, 248],
#          ...,
#          [ 27,  27,  27,  ...,  94, 106, 117]],
#         [[ 12,  10,  11,  ..., 253, 253, 253],
#          [ 10,  10,  12,  ..., 251, 252, 253],
#          ...,
#          [ 35,  35,  35,  ..., 214, 232, 223]]], dtype=torch.uint8)

# PyTorch tensor to PIL image
tpi = ToPILImage()
pilimage = tpi(pytorchimagetensor)

import matplotlib.pyplot as plt

plt.figure(figsize=[7, 9])
plt.title(label="s500_394origin_data", fontsize=14)
plt.imshow(X=pilimage)
plt.show()

Image description


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2025-05-13T23:35:01+00:00) Conversion with PIL image, PyTorch tensor & NumPy array. Retrieved from https://www.scien.cx/2025/05/13/conversion-with-pil-image-pytorch-tensor-numpy-array/

MLA
" » Conversion with PIL image, PyTorch tensor & NumPy array." Super Kai (Kazuya Ito) | Sciencx - Tuesday May 13, 2025, https://www.scien.cx/2025/05/13/conversion-with-pil-image-pytorch-tensor-numpy-array/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Tuesday May 13, 2025 » Conversion with PIL image, PyTorch tensor & NumPy array., viewed ,<https://www.scien.cx/2025/05/13/conversion-with-pil-image-pytorch-tensor-numpy-array/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » Conversion with PIL image, PyTorch tensor & NumPy array. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/05/13/conversion-with-pil-image-pytorch-tensor-numpy-array/
CHICAGO
" » Conversion with PIL image, PyTorch tensor & NumPy array." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2025/05/13/conversion-with-pil-image-pytorch-tensor-numpy-array/
IEEE
" » Conversion with PIL image, PyTorch tensor & NumPy array." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2025/05/13/conversion-with-pil-image-pytorch-tensor-numpy-array/. [Accessed: ]
rf:citation
» Conversion with PIL image, PyTorch tensor & NumPy array | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2025/05/13/conversion-with-pil-image-pytorch-tensor-numpy-array/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.