为什么即使阵列不是黑色,图像在保存时也是黑色的?

2024-04-19 16:15:31 发布

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

我试图保存从{{CD1>}数组中输入的黑白图像,但是图像保存为完全黑色,即使数组中有大于0的部分。

import scipy.io
from PIL import Image

importedImageList = [[0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0.32941176, 0.87058824, 0.2627451, 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0.7254902, 0.99607843, 0.44705882, 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0.62352941, 0.99607843, 0.28235294, 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0.59215686, 0.99607843, 0.44705882, 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0.23529412, 0.99607843, 0.63921569, 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0.23921569, 0.4745098, 0.4745098, 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0.14117647, 0.94509804, 0.89019608, 0.06666667, 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0.12156863, 0.52156863,
                      0.94901961, 0.99607843, 0.99607843, 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0.77647059, 0.99607843, 0.25882353, 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0.01176471, 0.14901961, 0.87843137, 0.99607843,
                      0.99607843, 0.99607843, 0.81176471, 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0.77647059, 0.88235294, 0.05490196, 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0.0745098, 0.79607843, 0.99607843, 0.99607843, 0.99607843,
                      0.99607843, 0.85882353, 0.07058824, 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0.77647059, 0.99607843, 0.2627451, 0.,
                      0., 0., 0., 0., 0., 0.,
                      0.29411765, 0.86666667, 0.99607843, 0.99607843, 0.45098039, 0.20392157,
                      0.20392157, 0.15686275, 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0.77647059, 0.99607843, 0.2627451, 0.,
                      0., 0., 0., 0., 0.03529412, 0.49411765,
                      0.98431373, 0.99607843, 0.85882353, 0.30196078, 0.00392157, 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0.77647059, 0.99607843, 0.2627451, 0.,
                      0., 0., 0.23137255, 0.52156863, 0.80392157,  0.99607843,
                      0.94117647, 0.65098039, 0.1372549, 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0.77647059, 0.98039216, 0.23137255, 0.,
                      0.08627451, 0.50588235, 0.97647059, 0.99607843, 0.97254902, 0.71372549,
                      0.22352941, 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0.77647059, 0.89803922, 0.08235294, 0.3254902,
                      0.91372549, 0.99607843, 0.99607843, 0.73333333, 0.22745098, 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0.77647059, 0.99607843, 0.9254902, 0.99215686,
                      1., 0.93333333, 0.24313725, 0.01960784, 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0.66666667, 0.99607843, 0.99607843, 0.81960784,
                      0.3254902, 0.17254902, 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0.20392157, 0.54901961, 0.41568627, 0.07058824,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ],
                     [0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., 0., 0.,
                      0., 0., 0., 0., ]]

importedImage = np.array(importedImageList)
print(importedImage)

img = Image.fromarray(importedImage, '1')
img.save('my.png')
img.show()

图像完全是黑色的,但中间应该有一个大圆点。在遵循建议后,我现在可以看到一些光,但图像的打印输出应该是一个数字(7),而是全部集中在打印图像的底部


Tags: fromio图像imageimportimgpilnp
1条回答
网友
1楼 · 发布于 2024-04-19 16:15:31

您有一个单通道浮点图像,并且图像的列显然存储在该列表中。要获得所需的图像,以下代码可能会有所帮助:

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

importedImageList = [[ ... ]]

# Transpose resulting array, because columns are stored in the list
importedImage = np.array(importedImageList).T

# Show image, explicitly set scale 0 ... 1
plt.imshow(importedImage, vmin=0, vmax=1, cmap='gray')
plt.colorbar()
plt.tight_layout()
plt.show()

# Explicitly convert to range 0 ... 255 and uint8 type for saving
img = Image.fromarray(np.uint8(importedImage * 255))
img.save('my.png')

情节如下所示:

Plot

这就是保存的图像:

Output

希望有帮助

                    
System information
                    
Platform:    Windows-10-10.0.16299-SP0
Python:      3.8.1
Matplotlib:  3.2.0rc3
NumPy:       1.18.1
Pillow:      7.0.0
                    

相关问题 更多 >