How do I write label images from Drake to disk using PIL
?
If I try Image.fromarray(label)
, I get an error that looks like this:
File ".../PIL/Image.py", line 2751, in fromarray
raise TypeError("Cannot handle this data type: %s, %s" % typekey) from e
TypeError: Cannot handle this data type: (1, 1, 1), <i2
Some image libraries, like
PIL
, OpenCV, etc., may require you to reshape your input and cast to a specificdtype
.Let's say you get your label image from the following notebook:
drake/tutorials/rendering_multibody_plant.ipynb
Option 1.a: PIL
For
PIL
(at least what's available on Ubuntu 18.04), you can usenp.int32
.Example:
See: https://github.com/python-pillow/Pillow/issues/2970
Option 1.b: OpenCV
For OpenCV (3.4.0), you can use
np.uint16
, in addition toIMREAD_UNCHANGED
:See: http://jamesgregson.ca/16-bit-image-io-with-python.html
Example screen capture of the notebook: \
Option 2: Use
*.npy
or*.pkl
If you're just loading / saving for Python itself, you can just use
np.save
orpickle
.