Reputation: 53
I'm doing an image processing task and I want to concat two sites of pictures. For concatting, I first converted the image to tensor, then converted the tensor to a PIL image to display it, but it was reported incorrectly.Could someone please help me? Here is my code:
import skimage.io as io
import torch
from torchvision import models, transforms
from PIL import Image
import matplotlib.pyplot as plt
from torchvision.transforms import ToPILImage
import numpy as np
from skimage import data_dir,io,color
coll1 = io.ImageCollection('F:\\code1/*.jpg')
coll2 = io.ImageCollection('F:\\code2/*.jpg')
a = torch.tensor(coll1)
print(a)
print(a.shape)
b = torch.tensor(coll2)
print(b)
print(b.shape)
c=torch.cat((a,b),1)
print(c.shape)
print(c)
img= transforms.ToPILImage()
img.show()
and here is the error code:
Traceback (most recent call last): File "F:/filelist.py", line 39, in img.show() AttributeError: 'ToPILImage' object has no attribute 'show'
Upvotes: 5
Views: 4798
Reputation: 2177
The ToPILImage
method accepts a tensor or an ndarray as input, source.
You will need to cast a single image tensor to the ToPILImage
method. From your post, I suspect you are passing batches of image tensor instead of one, hence the error.
Assumed if you want to visualize image from tensor c
,
img = transforms.ToPILImage()(c)
img.show()
Upvotes: 2