使用PIL从数组中绘制图像

2024-06-16 09:35:43 发布

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

我正在尝试使用PIL中的Image.fromarray将数组转换为图像,但我似乎无法获得我想要的形状。 这是我的密码:

def affichage(c, disp):
    for i in range(len(c)):
        for j in range(len(c)):
            if c[i][j]==0:
                disp[i][j]=[255,255,255]
            elif c[i][j]==1:
                disp[i][j]=[255,0,0]
            elif c[i][j]==2:
                disp[i][j]=[0,255,0]
            elif c[i][j]==3:
                disp[i][j]=[0,0,255]
    return disp

disp = affichage(c,disp)
img = Image.fromarray(disp, 'RGB')
img.save('affichage.png', 'png')
img.show()

我正在转换数组,其值范围为0到3 int RGB颜色,但如果我绘制数组,它会显示奇怪的颜色行,输出的颜色不相同 以下是输出图像:

Using Image.fromarray

Using Image.fromarray

Using matplotlib to plot the array

Using matplotlib to plot the array

如何获得与matplotlib相同的输出


Tags: in图像imageimgforlenpng颜色