PyTorch PIL to Tensor and vice versa

username: QuantScientist
cooked: <p>HI,<br>
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.</p>
<blockquote>
<p>import glob<br>
from PIL import Image</p>
<p>images=glob.glob(“/root/data/amz//train_small/*jpg”)<br>
for image in images:<br>
img = Image.open(image)<br>
trans = transforms.ToPILImage()<br>
trans1 = transforms.ToTensor()<br>
plt.imshow(trans(trans1(img)))</p>
</blockquote>
<p><img src="https://discuss.pytorch.org/uploads/default/original/2X/7/77381802d2674ed7b4fda5f9fa0ede3c5acbcea0.png" alt="amazonas" width="662" height="477"></p>

username: QuantScientist
cooked: <p>Anyone can answer this…?</p>

username: tom
cooked: <p><a href="http://pytorch.org/docs/master/torchvision/transforms.html#conversion-transforms" class="onebox" target="_blank" rel="nofollow noopener">http://pytorch.org/docs/master/torchvision/transforms.html#conversion-transforms</a></p>
<p>implies you’ll go from [0,255] to [0,1]…</p>

username: moskomule
cooked: <p>I think it’s because your input is PNG (or 4 channel).</p>
<pre><code class="lang-auto">plt.imshow(trans(trans1(img).convert("RGB")))
</code></pre>
<p>will work.</p>
<p>This will solve <a href="https://github.com/pytorch/vision/pull/189" rel="nofollow noopener">https://github.com/pytorch/vision/pull/189</a></p>

username: QuantScientist
cooked: <p>Thank you all,<br>
I have a working POC here:<br>
<a href="https://github.com/QuantScientist/Data-Science-PyCUDA-GPU/blob/master/day%2002%20PyTORCH%20and%20PyCUDA/PyTorch/09%2BPyTorch%2BKaggle%2BImage%2BData-set%2Bloading%2Bwith%2BCNN.ipynb" class="onebox" target="_blank" rel="nofollow noopener">https://github.com/QuantScientist/Data-Science-PyCUDA-GPU/blob/master/day%2002%20PyTORCH%20and%20PyCUDA/PyTorch/09%2BPyTorch%2BKaggle%2BImage%2BData-set%2Bloading%2Bwith%2BCNN.ipynb</a></p>

username: Qiao_Kang
cooked: <p>the same question.</p>
<p>the link has been dead<br>
i’d apprieciate if you post you method</p>

username: himahuja
cooked: <p><a href="https://bit.ly/2FdGYm0" rel="nofollow noopener">https://bit.ly/2FdGYm0</a> Here is the link to the above notebook.</p>

username: Mihail_DM
cooked: <p>This link is not working.</p>

username: dejanbatanjac
cooked: <p>This example worked for me:</p>
<pre><code class="lang-auto">print("t is: ", t.size())
from torchvision import transforms
im = transforms.ToPILImage()(t).convert("RGB")
display(im)
print(im)
print(im.size)
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/2X/b/b135be1eb008abdf7ef0f5ae7ab4e6f2d3c0bf6c.png" alt="image" data-base62-sha1="phFEDh1wF0cLZxIA4AK4Pvz5Dg8" width="619" height="209"></p>

username: mathematics
cooked: <p>I converted viceversa  as</p>
<pre><code class="lang-auto">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)

</code></pre>
<pre><code class="lang-auto">
(1200, 1200)
torch.Size([1, 3, 1200, 1200])
(1200, 1200)
</code></pre>
<p>thanks to ptrblk. <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

username: Andrea_Virgillito
cooked: <p>Thank you so much! You’ve made it very clear</p>

username: ADONIS_HAMMOUTENE
cooked: <p>The link doesn’t work</p>

username: tom
cooked: <p>Probably something like this would work:</p>
<p><a href="https://pytorch.org/vision/stable/generated/torchvision.transforms.ToTensor.html#torchvision.transforms.ToTensor" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/vision/stable/generated/torchvision.transforms.ToTensor.html#torchvision.transforms.ToTensor</a></p>