img
是PIL图片。下面是我尝试将数据导入ndarray时的终端输出。你认为这个错误是我做过的,还是那个有numpy的东西?
>>> img
<PIL.TiffImagePlugin.TiffImageFile image mode=I;16 size=1280x1080 at 0x110CB1560>
>>> img.getdata()
<ImagingCore object at 0x1105df7b0>
>>> np.array(img.getdata(), np.uint16)
fish: Job 1, 'python' terminated by signal SIGBUS (Misaligned address error)
答案 0 :(得分:0)
此代码适用于我,在Python 3.4,Numpy 1.9中:
import os
from PIL import Image
import numpy as np
def img_data_in_nd_array():
img_dir = 'img'
file_name = 'avatar_physical_attraction.jpg'
img = Image.open(os.path.join(img_dir, file_name))
print (img.getdata())
print (np.array(img.getdata(), np.uint16))
if __name__ == '__main__':
img_data_in_nd_array()
结果:
<ImagingCore object at 0x105b958f0>
[[ 53 152 254]
[ 53 152 254]
[ 53 152 254]
...,
[ 52 151 253]
[ 52 151 253]
[ 52 151 253]]
也许您可以提供出错的图像,因此我们可以检查是否存在问题。你有没有试过另一张图片?