HI,
I am reading an image using pil, then transforming it into a tensor and then back into PIL. The result is different from the original image.
import glob
from PIL import Image
images=glob.glob(“/root/data/amz//train_small/*jpg”)
for image in images:
img = Image.open(image)
trans = transforms.ToPILImage()
trans1 = transforms.ToTensor()
plt.imshow(trans(trans1(img)))
5 Likes
tom
(Thomas V)
August 17, 2017, 10:23am
3
2 Likes
moskomule
(Moskomule)
August 17, 2017, 3:39pm
4
I think it’s because your input is PNG (or 4 channel).
plt.imshow(trans(trans1(img).convert("RGB")))
will work.
This will solve https://github.com/pytorch/vision/pull/189
Qiao_Kang
(Qiao Kang)
October 24, 2018, 11:03am
6
the same question.
the link has been dead
i’d apprieciate if you post you method
1 Like
himahuja
(Himanshu Ahuja)
January 7, 2019, 11:29am
7
https://bit.ly/2FdGYm0 Here is the link to the above notebook.
Mihail_DM
(Mihail Dm)
February 17, 2019, 11:33am
8
This link is not working.
This example worked for me:
print("t is: ", t.size())
from torchvision import transforms
im = transforms.ToPILImage()(t).convert("RGB")
display(im)
print(im)
print(im.size)
18 Likes
I converted viceversa as
pil_img = Image.open(img)
print(pil_img.size)
pil_to_tensor = transforms.ToTensor()(img).unsqueeze_(0)
print(pil_to_tensor.shape)
tensor_to_pil = transforms.ToPILImage()(pil_to_tensor.squeeze_(0))
print(tensor_to_pil.size)
(1200, 1200)
torch.Size([1, 3, 1200, 1200])
(1200, 1200)
thanks to ptrblk.
28 Likes
Thank you so much! You’ve made it very clear
tom
(Thomas V)
April 24, 2023, 7:42pm
13
1 Like