如何从numpy数组读取图像到PIL图像? [英] How to read image from numpy array into PIL Image?

查看:94
本文介绍了如何从numpy数组读取图像到PIL图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过执行以下操作从使用PIL的numpy数组读取图像:

I am trying to read an image from a numpy array using PIL, by doing the following:

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数据库中.

I think this is because fromarray expects the shape to be (height, width, num_channels) however the array I have is in the shape (num_channels, height, width) as it is stored in this was in an lmdb database.

如何重塑图像以使其与Image.fromarray兼容?

How can I reshape the Image so that it is compatible with Image.fromarray?

推荐答案

您不需要重塑形状. rollaxis的用途是:

You don't need to reshape. This is what rollaxis is for:

Image.fromarray(np.rollaxis(img, 0,3))

这篇关于如何从numpy数组读取图像到PIL图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆