从数组

时间:2016-01-13 00:57:41

标签: python image-processing rgb pixel

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

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)  

1 个答案:

答案 0 :(得分:0)

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)

enter image description here