0

I want to convert a PIL image to a numpy array. Numpy's asarray function simply puts the image in a 0-dimensional array.

(Pdb) p img
<PIL.Image._ImageCrop image mode=RGB size=1024x0 at 0x106953560>
(Pdb) img.getdata()
<ImagingCore object at 0x104c97b10>
(Pdb) np.asarray(img.getdata())
array([], dtype=float64)
(Pdb) np.asarray(img)
array(<PIL.Image._ImageCrop image mode=RGB size=1024x0 at 0x106953560>, dtype=object)
(Pdb) np.asarray(img).shape
()
(Pdb) np.asarray(img, np.uint8)
*** SystemError: error return without exception set

The solutions on a similar SO question didn't work.

Community
  • 1
  • 1
Rose Perrone
  • 61,572
  • 58
  • 208
  • 243

1 Answers1

5

Your img has size 1024x0:

PIL.Image._ImageCrop image mode=RGB size=1024x0 at 0x106953560

That is an image with 0 height. Therefore, the resultant NumPy array is empty. To fix, crop the image so that it has a positive width and height.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677