Convert a tensor of images to PIL image

Hi,

I want to convert a tensor of images to PIL images.

import torch
import torchvision.transforms as transforms

tran1 = transforms.ToPILImage()
x = torch.randn(64, 3, 32, 32) # 64 images here
pil_image_single = tran1(x[0]) # this works fine
pil_image_batch = tran1(x) # this does not work

Can somebody tell me if there is any efficient way to do the final line without going through a loop?

Thanks

I don’t think there is a way to achieve this without a loop, since the underlying PIL.Image.fromarray operation expects a single numpy array, if I’m not mistaken.

1 Like