[PYTHON] How to resolve the error from toimage (from PIL.Image import fromarray as toimage)

for i in range(100):
  img = toimage(X_test[i])
  label = results[i].argmax()
  plt.subplot(10, 10, pos)
  plt.imshow(img)
  plt.axis('off')
  plt.title(cifar10_labels[label])
  pos += 1        
plt.show()

When I tried to display the image in Jupyter notebook by executing, I got the following error.

KeyError Traceback (most recent call last) /usr/local/lib/python3.6/dist-packages/PIL/Image.py in fromarray(obj, mode) 2415 typekey = (1, 1) + shape[2:], arr['typestr'] -> 2416 mode, rawmode = _fromarray_typemap[typekey] 2417 except KeyError:

KeyError: ((1, 1, 3), '<f4')

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last) 1 frames /usr/local/lib/python3.6/dist-packages/PIL/Image.py in fromarray(obj, mode) 2417 except KeyError: 2418 # print(typekey) -> 2419 raise TypeError("Cannot handle this data type") 2420 else: 2421 rawmode = mode

TypeError: Cannot handle this data type

Changing the code as follows solved the problem.

for i in range(100):
  scale = 255.0 / np.max(X_test[i])
  img = toimage(np.uint8(X_test[i]*scale))
  label = results[i].argmax()
  plt.subplot(10, 10, pos)
  plt.imshow(img)
  plt.axis('off')
  plt.title(cifar10_labels[label])
  pos += 1        
plt.show()

Recommended Posts

How to resolve the error from toimage (from PIL.Image import fromarray as toimage)
[Python] How to import the library
How to access the Datastore from the outside
How to operate Linux from the outside Procedure
How to measure line speed from the terminal
[Python] How to remove duplicate values from the list
[Reintroduction to python] How to import via the parent directory
Backtrader How to import an indicator from another file
How to post a ticket from the Shogun API
How to use import
How to collect tweets from tweetid as soon as possible (72000 tweets / hour)
How to do the initial setup from Django project creation
How to hit NAPALM from the Web (NetDevOpsSec reality solution)
In Django, how to abbreviate the long displayed string as ....
How to import NoteBook as a module in Jupyter (IPython)
How to use the generator
How to use the decorator
How to increase the axis
How to start the program
How to get followers and followers from python using the Mastodon API
How to extract the desired character string from a line 4 commands
How to get a job as an engineer from your 30s
How to calculate the autocorrelation coefficient
How to use the zip function
How to use SWIG from waf
How to read the SNLI dataset
How to get the Python version
About Python, from and import, as
Resolve VS Code unresolved import error
How to launch Explorer from WSL
How to overwrite the output to the console
[PyMC3] Resolve the Compilation Failed error
How to access wikipedia from python
How to convert from .mgz to .nii.gz
How to use the ConfigParser module
From the introduction of GoogleCloudPlatform Natural Language API to how to use it
How to resolve "No kernel of grammar Python found" error in Atom
attempted relative import beyond top-level package and how to resolve when angry
How to check local GAE from iPhone browser in the same LAN
How to deal with OAuth2 error when using Google APIs from Python
How to eliminate the conda activate error without annoying execution policy changes
How to build an application from the cloud using the Django web framework
How to pass values to JavaScript variables directly from the [Django] template tag