我需要帮助来弄清楚如何将图像转换为深褐色。这是我目前所拥有的…但它只会将所有东西都改成黑色和白色,再加上一点点棕色。我不知道我做错了什么:(
import image
def convertSepia(input_image):
grayscale_image = image.EmptyImage(input_image.getWidth(), input_image.getHeight())
for col in range(input_image.getWidth()):
for row in range(input_image.getHeight()):
p = input_image.getPixel(col, row)
R = p.getRed()
G = p.getGreen()
B = p.getBlue()
newR = (R * 0.393 + G * 0.769 + B * 0.189)
newG = (R * 0.349 + G * 0.686 + B * 0.168)
newB = (R * 0.272 + G * 0.534 + B * 0.131)
newpixel = image.Pixel(newR, newG, newB)
grayscale_image.setPixel(col, row, newpixel)
sepia_image = image.EmptyImage(input_image.getWidth(), input_image.getHeight())
for col in range(input_image.getWidth()):
for row in range(input_image.getHeight()):
p = grayscale_image.getPixel(col, row)
red = p.getRed()
if red > 140:
val = (R * 0.393 + G * 0.769 + B * 0.189)
else:
val = 0
green = p.getGreen()
if green > 140:
val = (R * 0.349 + G * 0.686 + B * 0.168)
else:
val = 0
blue = p.getBlue()
if blue > 140:
val = (R * 0.272 + G * 0.534 + B * 0.131)
else:
val = 0
newpixel = image.Pixel(val, val, val)
sepia_image.setPixel(col, row, newpixel)
return sepia_image
win = image.ImageWin() img = image.Image("luther.jpg")
sepia_img = convertSepia(img) sepia_img.draw(win)
win.exitonclick()
还有什么建议可以从这里出发吗?谢谢:)
只需操纵像素值,就可以将图像转换为棕褐色。下面是代码(免责声明:摘自this文章。)
原始图像
深褐色图像
您的灰度图像不是灰度图像。在灰度图像中,所有三个通道
r
,g
,b
都有相同的值。在打开paint并尝试验证代码是否有意义。在
固定这些线路:
只需使用}。在
r
、g
、b
的平均值,并将其放入newR
、newG
和{还有一些加权方法。只需谷歌搜索RGB到强度公式。在
相关问题 更多 >
编程相关推荐