枕头:奇怪的输出从nxxx3 numpy矩阵创建RGB图像

2024-05-16 00:00:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将nxnx3矩阵转换为RGB图像,其中矩阵中的每个空格都是[R,G,B]。我以为这很容易,但我遇到了一个奇怪的错误。在

我有这个图像:http://www.cs.brandeis.edu//~arya/test2.bmp

这只是一个10像素×10双像素位图,上面有一个红色的对角线,如你所见。在

下面是我的python代码:

import numpy as np
from PIL import Image
imm = Image.open("test2.bmp")
t = np.int32(imm)
print(t.shape) # (10,10,3)
new = Image.fromarray(t, mode="RGB")
new.save("test_output.bmp")

然后我得到这个图像:http://www.cs.brandeis.edu//~arya/test_out.bmp-与原始输出非常明显的不同。我会料到的图像.fromarray()为了塑造我的形象,我是不是错过了什么?在

谢谢你!在


Tags: 图像imageimporthttpwwwnp矩阵rgb