简体   繁体   中英

converting a 2d array to an image

I have this list of integers called (decrypted) that I want to show it as an image I tried to convert it to numpy array and show it using open cv2 but nothing seems to work

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, 19 9, 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]]

Three solutions are:

  1. using matplotlib :

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

在此处输入图片说明

  1. using PIL :

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

在此处输入图片说明

  1. using cv2 :

     import cv2 cv2.imwrite('decrypted3.png', decrypted) cv2.imshow("decrypted3", decrypted) cv2.waitKey() 

在此处输入图片说明

If you want to have a grayscale image, then simply convert it to numpy array with uint8 dtype as in:

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

On the other hand, if you want a color image (RGB), then stack three of those grayscale images along depth-wise:

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

Having these, you can then display using matplotlib or OpenCV or pillow etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM