PIL中的Image和numpy中的数组array相互转换

1. PIL image转换成array

     img = np.asarray(image)

需要注意的是,如果出现read-only错误,并不是转换的错误,一般是你读取的图片的时候,默认选择的是"r","rb"模式有关。

修正的办法: 手动修改图片的读取状态

  img.flags.writeable = True  # 将数组改为读写模式

 

2. array转换成image

Image.fromarray(np.uint8(img))

  

 

参考资料:

http://stackoverflow.com/questions/384759/pil-and-numpy

posted @ 2016-11-29 16:48  龚细军  阅读(44563)  评论(2编辑  收藏  举报