karolzak / ipyplot

IPyPlot is a small python package offering fast and efficient plotting of images inside Python Notebooks. It's using IPython with HTML for faster, richer and more interactive way of displaying big numbers of images.
MIT License
415 stars 41 forks source link

[Bug] "TypeError: Cannot handle this data type" when using numpy.ndarray with floats #6

Closed karolzak closed 4 years ago

karolzak commented 4 years ago

Getting an error when trying to run IPyPlot with images in numpy.ndarray format with float data type.

KeyError                                  Traceback (most recent call last)
~\Anaconda3\envs\py36tf2gpu\lib\site-packages\PIL\Image.py in fromarray(obj, mode)
   2644             typekey = (1, 1) + shape[2:], arr["typestr"]
-> 2645             mode, rawmode = _fromarray_typemap[typekey]
   2646         except KeyError:

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

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
<ipython-input-36-2d4541c259ee> in <module>
      4 imgs = np.random.rand(3,128,128,3).astype(np.float32)
      5 
----> 6 ipyplot.plot_images(imgs)

d:\git\ipyplot\ipyplot\plotting.py in plot_images(images, labels, max_images, img_width, force_b64)
     90         labels = list(range(0, len(images)))
     91     html = _create_imgs_list_html(
---> 92         images, labels, max_images, img_width, force_b64=force_b64)
     93 
     94     _display(html)

d:\git\ipyplot\ipyplot\plotting.py in _create_imgs_list_html(images, labels, max_images, img_width, force_b64)
    238     html = ''.join([
    239         _create_img_html(x, img_width, label=y, force_b64=force_b64)
--> 240         for x, y in zip(images[:max_images], labels[:max_images])
    241     ])
    242     return html

d:\git\ipyplot\ipyplot\plotting.py in <listcomp>(.0)
    238     html = ''.join([
    239         _create_img_html(x, img_width, label=y, force_b64=force_b64)
--> 240         for x, y in zip(images[:max_images], labels[:max_images])
    241     ])
    242     return html

d:\git\ipyplot\ipyplot\plotting.py in _create_img_html(image, width, label, force_b64)
    230     if use_b64:
    231         html += '<img src="data:image/png;base64,%s" style="margin: 1px; width: %spx; border: 2px solid #ddd;"/>' % (
--> 232             _img_to_base64(image, width*2), width)  # NOQA E501
    233 
    234     return html + '</div>'

d:\git\ipyplot\ipyplot\plotting.py in _img_to_base64(image, max_size)
    206 def _img_to_base64(image, max_size):
    207     if type(image) is np.ndarray:
--> 208         image = Image.fromarray(image)
    209     elif type(image) is str or type(image) is str_:
    210         image = Image.open(image)

~\Anaconda3\envs\py36tf2gpu\lib\site-packages\PIL\Image.py in fromarray(obj, mode)
   2645             mode, rawmode = _fromarray_typemap[typekey]
   2646         except KeyError:
-> 2647             raise TypeError("Cannot handle this data type")
   2648     else:
   2649         rawmode = mode

TypeError: Cannot handle this data type

Minimum repro:

import ipyplot
import numpy as np

# this will fail:
imgs = np.random.rand(3,128,128,3).astype(np.float32)
ipyplot.plot_images(imgs)

# this will work:
ipyplot.plot_images((imgs * 255).astype(np.uint8))