【问题标题】:Generate pixel image in Python from array在 Python 中从数组生成像素图像
【发布时间】:2016-04-09 05:40:37
【问题描述】:

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

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)  

【问题讨论】:

    标签: python image-processing rgb pixel


    【解决方案1】:
    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      • 2017-08-23
      • 2018-06-25
      相关资源
      最近更新 更多