我希望增强一种颜色(例如'红色'),其他颜色变为灰度(例如'蓝&绿')因此,输出数字预计为&#39 ; RGB'模式和' L' Python PIL lib中的模式。
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)
它仍然失败,只显示红色。我是否定义了错误或者我是否必须生成2个数字(RGB和灰色)并粘贴到一个?