Image. fromarray() throws up error TypeError: Cannot handle this data type

username: nbansal90
cooked: <p>I am trying to make a custom dataset, But as I convert it to PIL image, I am Getting the error:<br>
<strong>TypeError: Cannot handle this data type</strong></p>
<blockquote></blockquote>
<pre><code class="lang-auto">&gt; class Gen_Data(Dataset):
&gt; &gt;         def __init__(self, p, transform = None):
&gt; &gt;                 super(Gen_Data,self).__init__()
&gt; &gt;                 self.data = []
&gt; &gt;                 self.transform = transform
&gt; &gt; 
&gt; &gt;                 for v in p:
&gt; &gt;                         (self.data).append(v)
&gt; &gt; 
&gt; &gt;         def __getitem__(self, index):
&gt; &gt;                 img = np.asarray(self.data[index],dtype=np.uint8)
&gt; &gt;                 print (img)
&gt; &gt;                 print (type(img))
&gt; &gt;                 img = Image.fromarray(img.astype('uint8'))
&gt; &gt; 
&gt; &gt;                 if self.transform is not None:
&gt; &gt;                         img  = self.transform(img)
&gt; &gt; 
&gt; &gt;                 return (img)
&gt; &gt; 
&gt; &gt;         def __len__(self):
&gt; &gt;                 return len(self.data)
</code></pre>
<p>Where p is a numpy nd array.<br>
I am not quite understanding, how do I overcome this issue.</p>
<p><strong>Traceback (most recent call last):</strong><br>
**  File “/usr/local/torch3/lib/python3.5/site-packages/PIL/Image.py”, line 2428, in fromarray**<br>
**    mode, rawmode = _fromarray_typemap[typekey]**<br>
<strong>KeyError: ((1, 1, 64), ‘|u1’)</strong></p>
<p><strong>During handling of the above exception, another exception occurred:</strong></p>
<p><strong>Traceback (most recent call last):</strong><br>
**  File “pytorch_CelebA_DCGAN.py”, line 343, in **<br>
**    print (inception_score.inception_score(our_dataset, cuda=True, batch_size=32, resize=True, splits=10))**<br>
**  File “/home/bansa01/spectral_normalization/DCGAN-Pytorch2/DCGAN/inception_score.py”, line 50, in inception_score**<br>
**    for i, batch in enumerate(dataloader, 0):**<br>
**  File “/usr/local/torch3/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 259, in <strong>next</strong>**<br>
**    batch = self.collate_fn([self.dataset[i] for i in indices])**<br>
**  File “/usr/local/torch3/lib/python3.5/site-packages/torch/utils/data/dataloader.py”, line 259, in **<br>
**    batch = self.collate_fn([self.dataset[i] for i in indices])**<br>
**  File “pytorch_CelebA_DCGAN.py”, line 37, in <strong>getitem</strong>**<br>
**    img = Image.fromarray(img.astype(‘uint8’))**<br>
**  File “/usr/local/torch3/lib/python3.5/site-packages/PIL/Image.py”, line 2431, in fromarray**<br>
**    raise TypeError(“Cannot handle this data type”)**<br>
<strong>TypeError: Cannot handle this data type</strong></p>

username: 72f0243ccde5871a4325
cooked: <p>Have you handled this problem?</p>

username: KurtSunxx
cooked: <p>maybe you can input like img = Image.fromarray(np.uint8(img)) instead of img = Image.fromarray(img.astype(‘uint8’))</p>

username: scaramouche
cooked: <p>Hi there! May I ask what’s the difference between “np.uint8(img)” and “img.astype(‘uint8’)”? Cause I’ve been encountered this TypeError lately. Thanx a lot！</p>