I have numpy array with shape 101*101 with probabilities from 0 to 1 and I can plot numpy as image this way:
from PIL import Image
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
im = Image.fromarray(255*(preds1[1]))
plt.imshow(im)
Now I want now binarize image by some threshold this way:
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
But why?
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)