changeing only pixels that are predominantly red, rather than removing the red component of all pixels

时间:2016-11-26 18:32:12

标签: python-3.x

Please I want to change only pixels that are predominantly red, rather than removing the red component of all pixels. (I am using python 3.5) What might be wrong with my code.

from cImage import *
def removeRed(imageFile):
    myimagewindow = ImageWin("Image Processing",1000,500)   
    oldimage = FileImage(imageFile)
    oldimage.draw(myimagewindow)

    width = oldimage.getWidth()
    height = oldimage.getHeight()
    newim = EmptyImage(width,height)       

    for col in range(width):
        for row in range(height):
            old_pixel = oldimage.getPixel(col,row)
            new_pixel = Pixel(0, old_pixel.getGreen(), old_pixel.getBlue())
            newim.setPixel(col, row, new_pixel)
    newim.setPosition(width+1,0)
    newim.draw(myimagewindow)
    myimagewindow.exitOnClick()
removeRed("red.gif")

1 个答案:

答案 0 :(得分:0)

if you define "predominant" by "highest value of R,G,B is R" then just compare values and set to 0 only if red is higher than both green and blue:

for col in range(width):
    for row in range(height):
        old_pixel = oldimage.getPixel(col,row)
        if old_pixel.getRed() > old_pixel.getGreen() and old_pixel.getRed() > old_pixel.getBlue():
            new_pixel = Pixel(0, old_pixel.getGreen(), old_pixel.getBlue())
            newim.setPixel(col, row, new_pixel)
        else:
            newim.setPixel(col, row, old_pixel)