仅更改主要是红色的像素,而不是删除所有像素的红色分量

  • 本文关键字:红色 像素 删除 分量 python-3.x
  • 更新时间 :
  • 英文 :


请我只想更改主要红色的像素,而不是删除所有像素的红色组件。(我使用的是Python 3.5)我的代码可能出了问题。

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")

如果您定义"主要的"为" r,g,b是r"的最高值,则仅比较值并设置为0,仅当红色高于绿色和蓝色时:

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)

最新更新