Change specific RGB color pixels to another color, in image file

后端 未结 4 1904
不思量自难忘°
不思量自难忘° 2020-12-06 10:53

I would like to change a single color with Python.

If a fast solution with PIL exists, I would prefer this solution.

At the moment, I use

c         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 11:49

    I've just came up with this solution:

    import Image
    im = Image.open("MyImage.png")
    width, height = im.size
    colortuples = im.getcolors()
    mycolor1 = min(colortuples)[1]
    mycolor2 = max(colortuples)[1]
    pix = im.load()
    for x in range(0, width):
        for y in range(0, height):
            if pix[x,y] == mycolor1:
                im.putpixel((x, y), mycolor2)
    im.save('MyImage.png')
    

    Although putpixel isn't fast, it seems to be fast enough for me.

提交回复
热议问题