简体   繁体   English

从2D角度阵列创建彩色编码图像

[英]create a color coded image from 2D array of angles

i am trying to write some code in python relating angles to a certain colors. 我正在尝试在python中编写一些将角度与某些颜色相关的代码。 basically i have a 2d array full of angles where i looped through other arrays to perform calculations to arrive at ha_deg for each pixel 基本上我有一个充满角度的2d数组,在其中我遍历其他数组以执行计算以达到每个像素的ha_deg

ha_array[i,j] = ha_deg

and i want to turn this into an image with color gradients. 我想将其转换为具有颜色渐变的图像。 so if 0 is red, 180 is green and 360 is blue, i will be able to go pixel by pixel to assign a color to each angle. 因此,如果0是红色,180是绿色,360是蓝色,我将能够逐像素移动以为每个角度分配颜色。 sorry this is kind of an open ended question, I'm just not really sure how to proceed and could use some guidance! 抱歉,这是一个开放式问题,我只是不确定该如何进行,可以使用一些指导! thank you! 谢谢!

Here's a way to do this. 这是一种方法。 Below is a general function for generating linear RGB color gradients between two RGB values. 以下是用于在两个RGB值之间生成线性RGB颜色渐变的常规功能。 By combining color gradients of the appropriate lengths from red to green and from green to blue, one can make a list of RGB color values that can serve as a map between your angle values and the desired colors: 通过组合适当长度的颜色梯度(从红色到绿色以及从绿色到蓝色),可以制作一系列RGB颜色值,这些颜色值可以用作角度值和所需颜色之间的映射:

def generateColorGradient(RGB1, RGB2, n, includeEnd=False):
    dRGB = [float(x2-x1)/(n-1) for x1, x2 in zip(RGB1, RGB2)]
    gradient = [tuple([int(x+k*dx) for x, dx in zip(RGB1, dRGB)]) for k in range(n-1+includeEnd)]
    return gradient

gradient1 = generateColorGradient((255, 0, 0), (0, 255, 0), 181, includeEnd=False)
gradient2 = generateColorGradient((0, 255, 0), (0, 0, 255), 181, includeEnd=True)

gradient = numpy.array(gradient1 + gradient2)

#If `ha_array` is a numpy array, you can simply do this to convert all angles to RGB values:

color_array = gradient[ha_array]

#Then if you're using PIL to make images:

image = Image.fromarray(numpy.uint8(color_array))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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