绿色通道中的像素值为';在python中使用PIL时,与预期的不同
我正在试验,看看是否能从图像中得到像素值。但是我得到的图像值并不是我所期望的。(具体来说,绿色三角形的绿色通道给了我一个奇怪的值) 为了解决这个问题,我做了两个小实验 1) 我画了两个三角形:粉红色和红色。我得到了我期望的像素值 2) 我画了两个三角形:粉红色和绿色。我没有得到我期望的价值 因此,对于第一个场景,我事先就知道,我希望得到这些像素值: 使用这个简单的函数,我将绘制这些三角形:绿色通道中的像素值为';在python中使用PIL时,与预期的不同,python,numpy,colors,python-imaging-library,rgb,Python,Numpy,Colors,Python Imaging Library,Rgb,我正在试验,看看是否能从图像中得到像素值。但是我得到的图像值并不是我所期望的。(具体来说,绿色三角形的绿色通道给了我一个奇怪的值) 为了解决这个问题,我做了两个小实验 1) 我画了两个三角形:粉红色和红色。我得到了我期望的像素值 2) 我画了两个三角形:粉红色和绿色。我没有得到我期望的价值 因此,对于第一个场景,我事先就知道,我希望得到这些像素值: 使用这个简单的函数,我将绘制这些三角形: def get_pixel_vals(to_plot): im = Image.new("RGB
def get_pixel_vals(to_plot):
im = Image.new("RGB", (140,125))
for a_vertice in to_plot:
ImageDraw.Draw(im).polygon(a_vertice[1], outline=a_vertice[0], fill=None)
pix = np.array(im)
for channel_num, color_channel in enumerate(['red', 'green', 'blue']):
channel_list = []
channel_array = pix[..., channel_num] # gets you the R, G, or B channel of the numpy array
print '********* \n', color_channel, 'channel, min:', channel_array.min()
channel_list.append(channel_array.flatten().tolist())
channel_flat = [item for alist in channel_list for item in alist]
print color_channel, 'num unique values:', len(set(channel_flat))
print color_channel, 'values:', list(set(channel_flat))
imshow(im)
to_plot_pink_red = [ ['pink', [(25,25), (25,100), (50,100), ]] , ['red', [(100,25), (100,100), (125, 100)]] ]
get_pixel_vals(to_plot_pink_red)
我得到:
太好了!正是我所期望的
那么,如果我在绿色三角形旁边画一个粉红色三角形呢
我想
我差点就明白了…但不完全明白
to_plot_pink_green = [ ['pink', [(25,25), (25,100), (50,100), ]] , ['green', [(100,25), (100,100), (125, 100)]] ]
get_pixel_vals(to_plot_pink_green)
我希望G频道能有[0192255]
,但为什么我会得到[0192128]
?
为什么255变为128?您使用的颜色名称在中定义<代码>“绿色”被映射到
“#008000”
,这就是为什么在绿色通道中只看到128个。如果希望rgb值为0、255、0,可以使用“lime”
或仅使用“#00ff00”
中定义了您使用的颜色名称<代码>“绿色”被映射到“#008000”
,这就是为什么在绿色通道中只看到128个。如果希望rgb值为0、255、0,可以使用“lime”
或仅使用“#00ff00”
to_plot_pink_green = [ ['pink', [(25,25), (25,100), (50,100), ]] , ['green', [(100,25), (100,100), (125, 100)]] ]
get_pixel_vals(to_plot_pink_green)
*********
red channel, min: 0
red num unique values: 2
red values: [0, 255]
*********
green channel, min: 0
green num unique values: 3
green values: [0, 192, 128]
*********
blue channel, min: 0
blue num unique values: 2
blue values: [0, 203]