如何从numpy数组中读取图像到PIL图像?
作者:互联网
我正在尝试通过执行以下操作从使用PIL的numpy数组读取图像:
from PIL import Image
import numpy as np
#img is a np array with shape (3,256,256)
Image.fromarray(img)
并收到以下错误:
File "...Image.py", line 2155, in fromarray
raise TypeError("Cannot handle this data type")
我认为这是因为fromarray期望形状为(height,width,num_channels),但是我所拥有的数组的形状为(num_channels,height,width),因为它存储在lmdb数据库中.
如何重塑Image,使其与Image.fromarray兼容?
解决方法:
您无需重塑. This is what rollaxis is for:
Image.fromarray(np.rollaxis(img, 0,3))
标签:pillow,python,numpy,lmdb 来源: https://codeday.me/bug/20191010/1888163.html