Transforming numpy padded images to PIL images

username: Majid_Shirazi
cooked: <p>Hi everyone,</p>
<p>I first padded my PIL image list, for that reason I had to transform my PIL list into numpy-array. Now for doing torchvision transforms on my images I need to first transform my images back to PIL. But my images look like this:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/0/c07697dbb246a2e1deeef0b1c3e13a42a8648bd8.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/c07697dbb246a2e1deeef0b1c3e13a42a8648bd8" title="004"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/0/c07697dbb246a2e1deeef0b1c3e13a42a8648bd8_2_690x481.jpeg" alt="004" data-base62-sha1="rsBL3ThMneM5yLgoGdzaboEZp4A" width="690" height="481" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/0/c07697dbb246a2e1deeef0b1c3e13a42a8648bd8_2_690x481.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/c/0/c07697dbb246a2e1deeef0b1c3e13a42a8648bd8.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/0/c07697dbb246a2e1deeef0b1c3e13a42a8648bd8.jpeg 2x" data-dominant-color="DCDCDC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">004</span><span class="informations">796×555 114 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Is there anyone who could save me? <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

username: ptrblck
cooked: <p>Your screenshots are a bit hard to read and it’s better to post the code directly by wrapping it into three backticks ``` <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>Did you call <code>reshape</code> or <code>view</code> in order to swap some axes of the image?<br>
If looks like you are interleaving the image data.<br>
Note that you should call <code>x.permute(2, 0, 1)</code> e.g. to change a <code>[H, W, C]</code> tensor to <code>[C, H, W]</code>.</p>

username: Majid_Shirazi
cooked: <p>Sorry for bad image.</p>
<pre><code class="lang-auto">data_dir = "C:/Users/Majid/Desktop/data/LSP"   # Loading dataset
joints_dir = pjoin(data_dir, "joints")         
mat_file = sio.loadmat(joints_dir)             # Reading joints.mat
joints = mat_file['joints']
angle = []
image_list = []
for filename in glob.glob('C:/Users/Majid/Desktop/data/LSP/images/*.jpg'): # Reading images from dataset
    im=Image.open(filename)

    image_list.append(np.asarray(im))

image_list=np.array(image_list)
</code></pre>
<p>then i did padding like this:</p>
<pre><code class="lang-auto">i = 0
padded = []
shifted_x = []
shifted_y = []
for x in image_list:
    fram=np.zeros((y_max, x_max,3),dtype='uint8')        # Creating a blank space with dimension of maximum x and y
    # Location each images inside of each blank space with shifting to the center
    fram[int((y_max-y_shape[i])/2):y_shape[i]+int((y_max-y_shape[i])/2), int((x_max-x_shape[i])/2):x_shape[i]+int((x_max-x_shape[i])/2)] = x
    shifted_x.append(int((x_max-x_shape[i])/2))
    shifted_y.append(int((y_max-y_shape[i])/2))
    padded.append(fram)
    i = i+1
padded = np.array(padded)
padded = padded/255
</code></pre>
<pre><code class="lang-auto">images_data=[]
for j in padded:
    image_new = transforms.functional.to_pil_image(j,mode="RGB")
    images_data.append(image_new)
</code></pre>
<p>now when I plot it it gave me wrong picture. I also tried to change it to C<em>H</em>W, but it get worse.</p>

username: ptrblck
cooked: <p>Thanks for the code!<br>
I’ve debugged it and it seems <code>PIL</code> is unable to properly create an image out of the np array.<br>
In fact <code>Image.from_array(j, mode="RGB")</code> creates the same interleaved output.</p>
<p>You could fix it via:</p>
<pre><code class="lang-python">images_data=[]
for j in padded:
    j = j * 255.
    j = j.astype(np.uint8)
    image_new = transforms.functional.to_pil_image(j, mode="RGB")
    images_data.append(image_new)
</code></pre>
<p>I’m not sure, why the transformation fails using, but maybe it’s expected for <code>float64</code>.<br>
CC <a class="mention" href="/u/fmassa">@fmassa</a> who might know more about it.</p>