将2d数组转换为图像

问题描述 投票:1回答:2

我有一个名为(解密)的整数列表,我想将其显示为图像我试图将其转换为numpy数组并使用open cv2显示它但似乎没有任何工作

rowNo=img.shape[0]
colNo=img.shape[1]

decrypted=[]
for i in range(rowNo):
    row=[]
    for j in range(colNo):

        s=encrypted[i][j]

        s=s**d


        s=s%n

        row.append(s)
    decrypted.append(row)

[[136, 136, 135, 136, 135, 136, 136, 136, 127, 120, 118, 121, 130, 134, 135, 136, 134, 135, 135, 199, 199], [135, 136, 135, 136, 135, 136, 133, 124, 119, 119, 123, 132, 134, 135, 134, 135, 134, 134, 134, 199, 199], [136, 136, 135, 136, 136, 131, 121, 119, 119, 125, 132, 135, 135, 135, 134, 135, 135, 135, 134, 199, 199], [135, 136, 136, 136, 128, 120, 119, 119, 126, 133, 134, 136, 135, 135, 134, 135, 135, 135, 134, 199, 199], [136, 137, 133, 125, 119, 119, 120, 129, 134, 135, 134, 135, 136, 136, 135, 135, 135, 135, 134, 199, 199], [199, 189, 141, 121, 120, 121, 129, 134, 130, 128, 128, 129, 128, 134, 136, 135, 135, 135, 134, 199, 199], [120, 134, 181, 187, 123, 131, 134, 126, 123, 125, 124, 127, 129, 123, 130, 136, 135, 135, 135, 199, 199], [148, 141, 128, 156, 199, 141, 128, 124, 131, 129, 126, 123, 123, 128, 119, 133, 135, 135, 135, 199, 199], [153, 153, 156, 155, 154, 188, 124, 134, 137, 135, 132, 129, 124, 126, 119, 128, 136, 136, 136, 199, 199], [152, 157, 162, 164, 159, 185, 170, 178, 184, 173, 148, 133, 129, 123, 120, 126, 136, 136, 136, 199, 199], [157, 163, 164, 165, 165, 164, 178, 159, 162, 167, 190, 195, 142, 122, 122, 128, 137, 137, 133, 199, 199], [163, 164, 165, 165, 164, 165, 150, 135, 143, 147, 138, 148, 199, 142, 122, 134, 136, 130, 123, 199, 199], [165, 165, 165, 165, 165, 165, 165, 163, 160, 160, 157, 151, 134, 199, 137, 136, 126, 125, 132, 199, 199], [165, 165, 165, 165, 165, 165, 164, 165, 164, 162, 160, 157, 154, 159, 192, 124, 127, 134, 130, 199, 199], [164, 166, 165, 165, 165, 165, 165, 165, 165, 165, 164, 165, 164, 158, 198, 138, 132, 129, 131, 199, 199], [165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 165, 162, 155, 157, 194, 155, 129, 131, 124, 199, 199], [165, 165, 165, 166, 165, 165, 165, 165, 165, 165, 158, 155, 160, 162, 194, 163, 129, 122, 121, 199, 199], [165, 165, 164, 166, 165, 165, 165, 165, 162, 156, 156, 163, 160, 159, 195, 158, 120, 125, 128, 199, 199], [165, 165, 165, 165, 165, 165, 164, 159, 154, 160, 162, 159, 160, 160, 199, 138, 127, 129, 124, 199, 199], [159, 165, 165, 165, 165, 163, 158, 158, 163, 160, 157, 161, 157, 172, 197, 140, 126, 121, 109, 199, 199], [161, 164, 165, 165, 165, 166, 165, 164, 158, 159, 160, 155, 154, 199, 163, 126, 118, 109, 85, 101, 180], [199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 199, 138, 85, 85]]

python image list numpy matrix
2个回答
1
投票

如果你想拥有一个灰度图像,那么只需将其转换为带有uint8 dtype的numpy数组,如下所示:

grayscale = np.array(decrypted, dtype=np.uint8)

另一方面,如果你想要一个彩色图像(RGB),那么沿深度方向堆叠三个灰度图像:

decrypted_rgb = np.dstack([np.array(decrypted, dtype=np.uint8)]*3)

有了这些,你可以使用matplotlib或OpenCV或枕头等显示。


1
投票

三种解决方案是:

  1. 使用matplotlibfrom matplotlib import pyplot as plt plt.imshow(decrypted, interpolation='nearest', cmap='gray') plt.savefig('decrypted1.png') plt.show()

enter image description here

  1. 使用PILfrom PIL import Image img = Image.fromarray(decrypted.astype(np.uint8), 'L') img.save('decrypted2.png') img.show()

enter image description here

  1. 使用cv2import cv2 cv2.imwrite('decrypted3.png', decrypted) cv2.imshow("decrypted3", decrypted) cv2.waitKey()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.