Python 将PIL图像转换为numpy数组

Python 将PIL图像转换为numpy数组,python,numpy,python-imaging-library,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)

我想将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
(Pdb)p img
(Pdb)img.getdata()
(Pdb)np.asarray(img.getdata())
数组([],dtype=float64)
(Pdb)np.asarray(img)
数组(,dtype=object)
(Pdb)np.asarray(img).形状
()
(Pdb)np.asarray(img,np.uint8)
***SystemError:返回错误,未设置异常

a上的解决方案不起作用。

您的
img
大小为1024x0:

PIL.Image.\u ImageCrop Image mode=RGB size=1024x0在0x106953560处


这是一个高度为0的图像。因此,生成的NumPy数组为空。要修复,请裁剪图像,使其具有正宽度和正高度。

您的
img
大小为1024x0:

PIL.Image.\u ImageCrop Image mode=RGB size=1024x0在0x106953560处

这是一个高度为0的图像。因此,生成的NumPy数组为空。要修复,请裁剪图像,使其具有正宽度和正高度