gpt4 book ai didi

python - Pillow Image 对象和 numpy 数组之间的转换改变了维度

转载 作者:IT老高 更新时间:2023-10-28 20:28:56 24 4
gpt4 key购买 nike

我正在使用 Pillow 和 numpy,但是 Pillow Image 对象和 numpy 数组之间的转换出现问题。

当我执行以下代码时,结果很奇怪。

im = Image.open(os.path.join(self.img_path, ifname))
print im.size
in_data = np.asarray(im, dtype=np.uint8)
print in_data.shape

结果是

(1024, 768)
(768, 1024)

为什么尺寸会改变?

最佳答案

我可能是列优先的,而 numpy 中的数组是行优先的

in_data = in_data.T转置python数组

可能应该使用 matplotlibimshow 检查 in_data 以确保图片看起来正确。

但是你知道 matplotlib 自带加载函数,可以直接给你 numpy 数组吗?见:http://matplotlib.org/users/image_tutorial.html

关于python - Pillow Image 对象和 numpy 数组之间的转换改变了维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19016144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com