I need help figuring out how to convert an image to sepia. This is what I have so far..but it only changes everything to black and white colors with a very small tint of brown. I'm not sure what I'm doing wrong :(
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()
Any more tips as to where to go from here? Thanks :)
newpixel = image.Pixel(val, val, val)
. If all pixel channels are same, it becomes greyscale.