将PIL图像转换为numpy数组

Ros*_*one 0 python numpy python-imaging-library

我想将PIL图像转换为numpy数组.Numpy的asarray功能只是将图像放在一个0维数组中.

(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
Run Code Online (Sandbox Code Playgroud)

关于类似SO问题的解决方案不起作用.

unu*_*tbu 5

你的img尺码是1024x0:

PIL.Image._ImageCrop图像模式= RGB大小= 1024x0,位于0x106953560

这是一个0高的图像.因此,结果NumPy数组为空.要进行修复,请裁剪图像,使其具有正宽度和高度.