【问题标题】:Image from array not showing the correct result数组中的图像未显示正确的结果
【发布时间】:2020-06-13 19:31:17
【问题描述】:

我正在生成点阵的二进制配置,我想将它们保存为黑白图像。我在 Python 3.7 上使用以下代码:

import numpy as np
from matplotlib import pyplot as plt
from PIL import Image

L = 10
s = 2*np.random.randint(2, size=(L, L))-1
s = (s + 1) * 255/ 2
s.astype(np.uint8)
img = Image.fromarray(s, mode = 'L')
plt.imshow(img, cmap='gray')
plt.show()

img.save('test.jpg')

img2 = Image.open('test.jpg').convert('L')
data = asarray(img2).astype(int)
print(data)

这是我生成的随机数组:

[[255.   0.   0.   0.   0. 255. 255.   0.   0. 255.]
 [  0.   0.   0. 255.   0. 255.   0.   0. 255.   0.]
 [  0.   0.   0. 255. 255. 255.   0. 255.   0. 255.]
 [255. 255. 255. 255. 255. 255.   0. 255. 255. 255.]
 [255. 255.   0.   0.   0.   0. 255.   0. 255.   0.]
 [255. 255.   0.   0. 255.   0. 255.   0.   0.   0.]
 [  0.   0. 255. 255.   0. 255. 255.   0.   0.   0.]
 [  0. 255.   0. 255.   0. 255. 255.   0. 255.   0.]
 [  0. 255.   0.   0. 255. 255. 255. 255. 255. 255.]
 [  0. 255.   0.   0. 255.   0. 255. 255.   0.   0.]]

但代码输出的图像与我预期的完全不同。我得到的结果是以下图像,似乎与该数组没有任何关系:

obtained image

虽然我期待这个

expected image

我从 Mathematica 中的数组可视化中得到的。请注意,在代码末尾,我保存并检索图像。当我将它作为数组检索时,我获得了

[[  0   3   0  11   0 216 106  67   0   3]
 [  0   5   0  10   2   7   7   0   0   1]
 [  2   0   7   0   0   1   0   0   1   0]
 [ 10   0   0   3   8   0   8  14   3   3]
 [  0  12   0  21   0 219 111  43   0   0]
 [  4   8   0 206 114  86   0  15   1   1]
 [  0   0  22   0   8   0   0   7   7   0]
 [  6   0   0   9   0   8   6 221 102  81]
 [  0   0   0   0   0   0   0   0   0   0]
 [  0   0   0   0   0   0   0   0   0   0]]

与原来的数组s完全不同。如果有人能指出我做错了什么,我将不胜感激。

【问题讨论】:

    标签: python image grayscale


    【解决方案1】:

    这是由于plt.imshow 使用的颜色图。默认情况下,它将显示从深蓝色到亮黄色的灰度图像。尝试添加一个可选参数,称为cmap

    plt.imshow(img ,cmap='gray')
    

    【讨论】:

    • 感谢您的回答。这解决了在灰度上绘制它的问题,但我真正担心的是图像不像数组那样是二进制的。虽然数组只有两个值,0 和 255,但即使是 img 的灰度图也有两个以上的值。我正在编辑问题以澄清这一点。
    【解决方案2】:

    有两个问题:

    • 您将其保存为有损 JPEG - 允许更改像素以节省空间。如果您关心像素是否准确保存,请使用无损格式,例如 PNG

    • 你没有保存更改dtype的效果,所以你使用的变量仍然是一个浮点数-我在下面的代码中对其进行了注释


    #!/usr/bin/env python3
    
    import numpy as np
    from PIL import Image
    
    # Generate random but repeatable lattice
    np.random.seed(42)
    L = 10
    s = 2*np.random.randint(2, size=(L, L))-1
    s = (s + 1) * 255/ 2
    sA = s.astype(np.uint8)     # <--- YOUR CODE GOES WRONG HERE
    
    # Now make into "PIL Image" and save as lossless PNG (not JPEG)
    imA = Image.fromarray(sA)
    imA.save('result.png')
    imA.show()
    
    # Now load back from disk
    imB = Image.open('result.png')
    sB  = np.array(imB)
    imB.show()
    
    # Check we get the same Numpy array
    if np.allclose(sA, sB):
        print('Arrays are equal')
    else:
        print('ERROR: Arrays differ')
    

    【讨论】:

    • 谢谢,但我将文件的扩展名更改为 PNG,同样的事情发生了。除了文件扩展名中的 JPG -> PNG 之外,我还需要在代码中添加任何其他修改吗?
    • 我已经更新了我的答案,因为我有时间更彻底地检查您的代码。请再看看。
    • 我理解错误。现在工作正常。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 2010-11-14
    相关资源
    最近更新 更多