Python中每像素着色的图像

时间:2018-04-03 11:59:27

标签: python-3.x image

我需要256的着色图像256来自矩阵的值。

示例:

我有一个矩阵[[-64,-64,-64],[ - 8,10,8],[10,50,22]]和这个值的颜色,-64 =(0,0) ,0,255)-8 =(25,25,25,255)10 =(45,255,255,255)等。

如何通过Image.fromarray(color_matrix.astype(' uint8'),' RGBA')快速填充output_matrix和color tuppels并形成图像?

功能

def draw_tile(matrix: np.matrix, type: str, img_size: tuple) -> Image:
"""
Draws an image with the given data as the image size and the size of its   values for x and y.

:param matrix: numpy matrix with all measurement data
:param type: descriptor defining how to draw the input data
:param img_size: image size
:return: image
"""

def doppler_var_dict(var: float):
    return {
        var <= -30: 13,
        -30 < var <= -25: 0,
        -25 < var <= -20: 1,
        -20 < var <= -15: 2,
        -15 < var <= -10: 3,
        -10 < var <= -5: 4,
        -5 < var <= 0: 5,
        0 < var <= 1: 6,
        1 < var <= 5: 7,
        5 < var <= 10: 8,
        10 < var <= 15: 9,
        15 < var <= 20: 10,
        20 < var <= 25: 11,
        25 < var <= 30: 12,
        var >= 30: 14,
    }[1]

def rainfall_var_dict(var: float):
    return {
        var <= 0.2: 0,
        0.2 < var <= 0.5: 1,
        0.5 < var <= 1.5: 2,
        1.5 < var <= 2.5: 3,
        2.5 < var <= 4: 4,
        4 < var <= 6: 5,
        5 < var <= 10: 6,
        10 < var <= 15: 7,
        15 < var <= 20: 8,
        20 < var <= 35: 9,
        35 < var <= 50: 10,
        50 < var <= 80: 11,
        80 < var <= 120: 12,
        120 < var <= 200: 13,
        200 < var <= 300: 14,
        var >= 300: 1
    }[1]

def reflectivity_var_dict(var: float):
    return {
        var <= -4: 0,
        -4 < var <= -3.5: 1,
        -3.5 < var <= -3: 2,
        -3 < var <= -2.5: 3,
        -2.5 < var <= -.5: 4,
        -.5 < var <= -0: 5,
        -0 < var <= .25: 6,
        .25 < var <= 0.5: 7,
        .5 < var <= 1: 8,
        1 < var <= 1.25: 9,
        1.25 < var <= 1.5: 10,
        1.5 < var <= 2: 11,
        2 < var <= 2.5: 12,
        2.5 < var <= 3: 13,
        3 < var <= 3.5: 14,
        3.5 < var <= 4: 15,
        4 < var <= 5: 16,
        5 < var <= 6: 17,
        var >= 6: 18
    }[1]

doppler_color = [(0, 0, 0, 0), (55, 255, 195, 150), (0, 250, 255, 150), (0, 195, 255, 150),
                 (0, 100, 255, 150), (0, 0, 255, 150), (140, 140, 140, 150), (150, 0, 0, 150), (255, 0, 0, 150),
                 (255, 85, 0, 150), (255, 165, 0, 150), (255, 165, 80, 150), (255, 230, 130, 150),
                 (65, 65, 65, 150),
                 (255, 255, 0, 150)]

rainfall_color = [(0, 0, 0, 0), (200, 200, 200, 150), (180, 180, 255, 150), (120, 120, 255, 150),
                  (20, 20, 255, 150), (0, 216, 195, 150), (0, 150, 144, 150), (0, 102, 102, 150),
                  (255, 255, 0, 150),
                  (255, 200, 0, 150), (255, 150, 0, 150), (255, 100, 0, 150), (255, 0, 0, 150), (200, 0, 0, 150),
                  (120, 0, 0, 150), (40, 0, 0, 150)]

z_d_r_color = [(90, 0, 150, 150), (115, 0, 255, 150), (213, 0, 255, 150), (255, 0, 0, 150), (176, 0, 0, 150),
               (255, 85, 0, 150), (255, 220, 0, 150), (119, 255, 0, 150), (0, 255, 255, 150), (0, 255, 162, 150),
               (0, 162, 255, 150), (0, 0, 255, 150), (255, 0, 77, 150), (50, 2, 163, 150), (173, 173, 173, 150),
               (145, 145, 145, 150), (120, 120, 120, 150), (92, 92, 92, 150), (60, 60, 60, 150), (0, 0, 0, 0)]

z_d_r_color = list(reversed(z_d_r_color))

dict_dicts = {
    DOPPLER_RADIAL: doppler_var_dict,
    RADAR_RAINFALL: rainfall_var_dict,
    HORIZONTAL: reflectivity_var_dict,
    DIFFERENTIAL: reflectivity_var_dict
}

color_map_dict = {
    DOPPLER_RADIAL: doppler_color,
    RADAR_RAINFALL: rainfall_color,
    HORIZONTAL: z_d_r_color,
    DIFFERENTIAL: z_d_r_color
}

var_dict = dict_dicts[type]

try:
    color_shem = color_map_dict[type]
except KeyError as ee:
    return HttpResponseServerError('Error in bufr_image: wrong data type')

with timer.Profiler('color_matrix time'):
    color_matrix = np.array(list(map(lambda var: color_shem[var_dict(var)], matrix.reshape(-1))))
    color_matrix = color_matrix.reshape((img_size[0], img_size[1], 4))

img = Image.fromarray(color_matrix.astype('uint8'), 'RGBA')

return img

你能否建议一种算法能在不到一秒的时间内完成矩阵256乘256?

0 个答案:

没有答案