【发布时间】:2018-10-05 03:46:56
【问题描述】:
我需要编写一个程序,它读取红色、绿色和蓝色值并将该值添加到图像中的每个像素以调整颜色。
这是一个示例,我将 40 添加到每个像素的绿色值,但不向红色和蓝色通道添加任何内容:
File name: dragonfly.png
Red tint: 0
Green tint: 40
Blue tint: 0
我的代码在下面,它可以运行。但是当我提交它时,它说“提交创建了输出图像 output.png,但它与预期的输出图像不匹配”。我附上了两张图片 - 实际的和预期的。
请看我的代码:
import Image
file = input("File name: ")
red_tint = int(input("Red tint: "))
green_tint = int(input("Green tint: "))
blue_tint = int(input("Blue tint: "))
img = Image.open(file)
r,g,b = img.getpixel( (0,0) )
for y in range(img.height):
for x in range(img.width):
current_color = (r,g,b)
if current_color == r:
R = r + red_tint
if current_color == g:
G = g + green_tint
if current_color == b:
B = b + blue_tint
R, G, B = current_color
new_color = (R, G, B)
img.putpixel((x, y), new_color)
img.save('output.png')
我在代码中做错了什么?谢谢
【问题讨论】:
-
您的程序输出的文件看起来像预期的结果吗?另外:R、G 和 B 应该在你的循环中是什么?您实际上从未在代码中获得像素 (x, y)。看起来您只是将它们都设置为相同的值。
-
R,G,B 应该是 red,green,blue 的新值
-
那么它们应该从旧值派生而来。您应该在循环中从像素 (x,y) 获取 rgb 值。
-
@GarrettGutierrez 你能解释一下如何做吗?无论我一直试图做什么 - 都行不通。谢谢