PIL TypeError: Cannot handle this data type for volumetric images

Push: <p>Hello I am working with volumetric image data and getting following error. Any help will be appreciated. Thanks!!<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/9/89c794ce0d0c49cef5fac88cdb8a982a58654d8e.png" data-download-href="https://discuss.pytorch.org/uploads/default/89c794ce0d0c49cef5fac88cdb8a982a58654d8e" title="Screenshot from 2021-09-05 16-50-05"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/9/89c794ce0d0c49cef5fac88cdb8a982a58654d8e_2_470x375.png" alt="Screenshot from 2021-09-05 16-50-05" data-base62-sha1="jER0T9DMkImxEH98t55LHd5cKGq" width="470" height="375" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/9/89c794ce0d0c49cef5fac88cdb8a982a58654d8e_2_470x375.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/9/89c794ce0d0c49cef5fac88cdb8a982a58654d8e_2_705x562.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/8/9/89c794ce0d0c49cef5fac88cdb8a982a58654d8e.png 2x" data-dominant-color="F4F6F6"><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">Screenshot from 2021-09-05 16-50-05</span><span class="informations">807×643 89.1 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>

zetyquickly: <p>Hi <a class="mention" href="/u/push">@Push</a></p>
<p>Seems like this issue has nothing related to PyTorch.</p>
<p>But at a glance I see that <code>PIL</code> is complaining about wrong data type. It works with 2d or 3d arrays, where the number of channels comes in the last position and should be within 2,3 or 4.</p>

Push: <p>Thanks <a class="mention" href="/u/zetyquickly">@zetyquickly</a> for your reply. Yes, error is nothing to do with PyTorch. Please take a look at following image.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/5/c520557a7830883f38d9ea7ff74e9fa3cab2b904.png" data-download-href="https://discuss.pytorch.org/uploads/default/c520557a7830883f38d9ea7ff74e9fa3cab2b904" title="Screenshot from 2021-09-06 11-46-57"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c520557a7830883f38d9ea7ff74e9fa3cab2b904_2_690x459.png" alt="Screenshot from 2021-09-06 11-46-57" data-base62-sha1="s7RkSquOksPTmo30OZl26ZZ2Ya8" width="690" height="459" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/5/c520557a7830883f38d9ea7ff74e9fa3cab2b904_2_690x459.png, https://discuss.pytorch.org/uploads/default/original/3X/c/5/c520557a7830883f38d9ea7ff74e9fa3cab2b904.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/5/c520557a7830883f38d9ea7ff74e9fa3cab2b904.png 2x" data-dominant-color="F5F7F6"><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">Screenshot from 2021-09-06 11-46-57</span><span class="informations">897×597 89.4 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>Since, I am using gray scale image channel number is 1 and is specified in the last position. Still gives the same error. What is wrong with that? Thanks again!</p>

zetyquickly: <p><code>PIL</code> won’t work with tensor of size <code>(40,40,40) </code>and nor with <code>(40,40,40,1)</code>.</p>
<p>It takes tensors an treat them like image objects. The sizes acceptable are <code>(X,Y), (X,Y,1), (X,Y,3), (X,Y,4)</code>, where the  <code>X,Y</code> are height and width of your image</p>
<p>You can read each element of your <code>(40,40,40)</code> shaped tensor, if you pass <code>arr[i]</code> to <code>fromarray()</code></p>
<p>And you are right, this is not related to <code>PyTorch</code>. And it will be difficult to find an answer on <code>PyTorch</code> forum</p>