【问题标题】:How to convert wand.image.Image to PIL.Image?如何将 wand.image.Image 转换为 PIL.Image?
【发布时间】:2021-09-14 19:43:56
【问题描述】:
我花了一整天的时间在这个问题上,并没有在堆栈溢出中看到答案!
我试过了,但没有用:
>> pil_image = Image.frombytes('RGBA', wand_image.size, wand_image.make_blob(format='png'), 'raw')
ValueError: not enough image data
我感谢每一个解决方案。
【问题讨论】:
标签:
python
type-conversion
python-imaging-library
wand
【解决方案1】:
这里不涉及numpy:
pil_image = PIL.Image.open(io.BytesIO(wand_image.make_blob("png"))
【解决方案2】:
这对我有用:
img_buffer = numpy.asarray(bytearray(wand_img.make_blob(format='png')), dtype='uint8')
bytesio = io.BytesIO(img_buffer)
pil_img = PIL.Image.open(bytesio)
【解决方案3】:
一种方法是通过 numpy - 意思是将 PIL 图像导出到 numpy 数组中,然后通过 wand 读取它
from wand.image import Image
from IPython.display import display
with Image.from_array(np.array(img)) as ximg:
display(ximg)
反之亦然
from wand.image import Image
from matplotlib import cm
with Image(filename='rose:') as img:
array = np.array(img)
im = Image.fromarray(np.uint8(cm.gist_earth(array)*255))