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