0
我需要帮助了解如何将图像转换为棕褐色。这是我到目前为止......但它只是将一切变成黑色和白色,并带有非常小的棕色色调。我不知道我做错了什么:(在Python中处理图像为棕褐色色调
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()
任何更多的技巧在何处走?谢谢:)
那么我应该先把它转换成灰度?我的newR,newG和newB的值有什么问题?我很困惑:^( –
@KaileeCollins为什么你会命名变量灰度图像? 你的值的问题是,他们不会导致灰度图像 – Piglet
也许命名只是困惑我。确实是它会将RGB图像转换为一个名为grayscale_image的棕褐色图像,然后它会创建一个名为sepia_img的灰度图像,我完全理解你的困惑:) – Piglet