Pic混合了Python-PIL中的灰色和RGB成分

2024-04-29 04:05:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我想增强一种颜色(例如“红色”),而另一种颜色变为灰度(例如“蓝色和绿色”),因此,在Python PIL lib中,输出数字应为“RGB”模式和“L”模式。你知道吗

import PIL.Image as pim
file=input('Enter name of image to highlight: ')
colour=input('Enter colour to highlight: ')
multiplier=input('Enter multiplier: ')
m=float(multiplier)
original = pim.open(file)
highlight = pim.new("RGB", original.size)

highlight=original.convert('L')
#convert whole figure into greyscale

def high_red(r,g,b,m,x,y):
    if r>max(m*g,m*b):
        boosted_r=2*r-r**2/255
        reduced_g=g**2/255
        reduced_b=b**2/255
        colour=(int(boosted_r),int(reduced_g),int(reduced_b))
        highlight.putpixel((x, y), colour)
for x in range(original.width):
    for y in range(original.height):
        (r, g, b) = original.getpixel((x, y))
        if colour == 'Red':
            high_red(r,g,b,m,x,y)

但是,输出仅为灰度级,没有红色部分。我也试过一个接一个的把灰色部分写成

colour=round(0.2126*r+0.7152*g+0.0722*b)
highlight.putpixel((x, y), colour)

它仍然失败,只显示红色。我是否定义了错误的内容,还是必须生成两个图形(RGB和灰色)并粘贴到一个图形中?你知道吗


Tags: inputpil颜色模式rgbfileintenter