Ошибка типа: не удается обработать этот тип данных, пытаясь получить двоичное изображение из numpy - PullRequest
0 голосов
/ 19 октября 2018

У меня есть массив NumPy с формой 101 * 101 с вероятностями от 0 до 1, и я могу построить NUMPY как изображение таким образом:

from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
im = Image.fromarray(255*(preds1[1])) 
plt.imshow(im)

enter image description here

Теперь я хочу теперь преобразовать изображение в двоичную форму следующим образом:

im = Image.fromarray(255*(preds1[1] > threshold_best)) 

Going to return this error:
KeyError                                  Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/PIL/Image.py in fromarray(obj, mode)
   2459             typekey = (1, 1) + shape[2:], arr['typestr']
-> 2460             mode, rawmode = _fromarray_typemap[typekey]
   2461         except KeyError:

KeyError: ((1, 1), '<i8')

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-74-aea94c7b1043> in <module>()
      2 import matplotlib.pyplot as plt
      3 import matplotlib.image as mpimg
----> 4 im = Image.fromarray(255*(preds1[1] > threshold_best))
      5 plt.imshow(im)

~/anaconda3/lib/python3.6/site-packages/PIL/Image.py in fromarray(obj, mode)
   2461         except KeyError:
   2462             # print(typekey)
-> 2463             raise TypeError("Cannot handle this data type")
   2464     else:
   2465         rawmode = mode

TypeError: Cannot handle this data type

Но почему?

preds1[1].shape, 1*(preds1[1] > 0.4).shape
(101, 101), (101, 101)

type(preds1[1]), type(1*(preds1[1] > threshold_best))
(numpy.ndarray, numpy.ndarray)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...