title: Corrupted image is being saved with PIL
body: <p>I am having an issue whereby manipulating image pixels is causing a corrupted image to be saved...</p>
  
  <p>So, I open an image using PIL, and then convert it to a NumPy array:</p>
  
  <pre><code>image = Image.open("myimage.png")
  np_image = np.asarray(image)
  </code></pre>
  
  <p>Then, I transpose the image, to convert it from <code>[x][y][channel]</code> to <code>[channel][x][y]</code>:</p>
  
  <pre><code>pixels = np.transpose(np_image, (2, 0, 1))
  </code></pre>
  
  <p>If I then transpose this image back to <code>[x][y][channel]</code>, create a PIL image from this array, and then save the image:</p>
  
  <pre><code>image1 = np.transpose(pixels, (1, 2, 0))
  image2 = Image.fromarray(image1, 'RGB')
  image2.save('image2.png')
  </code></pre>
  
  <p>Then the image saved is identical to "myimage.png".</p>
  
  <p>However, if instead of the above code, I first assign <code>pixels</code> to an element in an array of images:</p>
  
  <pre><code>images = np.zeros([10, 3, 50, 50]) # The images are 50x50 with 3 channels
  images[0] = pixels
  image3 = np.transpose(images[0], (1, 2, 0))
  image4 = Image.fromarray(image3, 'RGB')
  image4.save('image4.png')
  </code></pre>
  
  <p>Then "image4.png" is corrupted. It appears as follows:</p>
  
  <p><a href="https://i.stack.imgur.com/IfJrM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IfJrM.png" alt="enter image description here"></a></p>
  
  <p>Whereas "myimage.png" is actually:</p>
  
  <p><a href="https://i.stack.imgur.com/NsK4T.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NsK4T.png" alt="enter image description here"></a></p>
  
  <p>So why is it that if I save the image when I directly transpose <code>pixels</code>, the image saved is as expected, but when I set <code>pixels</code> to the first element in the array <code>images</code>, and then transpose this image, the saved image is corrupted?</p>
  
  <p>Thanks!</p>