从数组中生成 Python 中的像素图像

Generate pixel image in Python from array

我正在尝试找出不同的方法来可视化数据以及如何掌握图像处理。

Python 2.6: Creating image from array 似乎在做一些不同的事情

我想到了一个简单的例子,我可以从头开始创建一个像素强度不同的图像。我使用 A_pixelIntensity 数组来声明像素颜色的强度。然后,我在 create_pixel_gradient 函数中进行转换,使强度较高的像素更暗,强度较低的像素更白。

如何查看此图片? matplotlib.pyplot 是最好的方法吗? PIL.ImageiPython.display 呢?

import numpy as np
#Intensity of Image
A_pixelIntensity = np.asarray([[1,1,1,1,1],[1,2,2,2,1],[1,2,3,2,1],[1,2,2,2,1],[1,1,1,1,1]])
# [[1 1 1 1 1]
#  [1 2 2 2 1]
#  [1 2 3 2 1]
#  [1 2 2 2 1]
#  [1 1 1 1 1]]

#Color of Image
def create_pixel_gradient(A_intensity):
    maximum = np.amax(A_intensity)
    init = np.zeros(A_intensity.shape,dtype=tuple) #Create empty array to populate
    for i in range(A_intensity.shape[0]):
        for j in range(A_intensity.shape[1]):
            pixel_intensity = A_intensity[i,j] #Get original pixel intensities
            value = 255 - int(pixel_intensity*(255/np.amax(A_intensity))) #Lower intensities are more white (creates a gradient)
            init[i,j] = (value,255,value) #Populate array with RGB values
    return(init)

create_pixel_gradient(A_pixelIntensity)
# array([[(170, 255, 170), (170, 255, 170), (170, 255, 170), (170, 255, 170),
#         (170, 255, 170)],
#        [(170, 255, 170), (85, 255, 85), (85, 255, 85), (85, 255, 85),
#         (170, 255, 170)],
#        [(170, 255, 170), (85, 255, 85), (0, 255, 0), (85, 255, 85),
#         (170, 255, 170)],
#        [(170, 255, 170), (85, 255, 85), (85, 255, 85), (85, 255, 85),
#         (170, 255, 170)],
#        [(170, 255, 170), (170, 255, 170), (170, 255, 170), (170, 255, 170),
#         (170, 255, 170)]], dtype=object)  
A_pixelIntensity = np.asarray([[1,1,1,1,1],[1,2,2,2,1],[1,2,3,2,1],[1,2,2,2,1],[1,1,1,1,1]])

plt.imshow(A_pixelIntensity)