PIL Best Way To Replace Color?

后端 未结 6 1954
时光取名叫无心
时光取名叫无心 2020-12-04 20:06

I am trying to remove a certain color from my image however it\'s not working as well as I\'d hoped. I tried to do the same thing as seen here Using PIL to make all white pi

6条回答
  •  臣服心动
    2020-12-04 20:54

    #!/usr/bin/python
    from PIL import Image
    import sys
    
    img = Image.open(sys.argv[1])
    img = img.convert("RGBA")
    
    pixdata = img.load()
    
    # Clean the background noise, if color != white, then set to black.
    # change with your color
    for y in xrange(img.size[1]):
        for x in xrange(img.size[0]):
            if pixdata[x, y] == (255, 255, 255, 255):
                pixdata[x, y] = (0, 0, 0, 255)
    

提交回复
热议问题