开发者

Replacing a single color in PIL?

开发者 https://www.devze.com 2023-01-05 19:10 出处:网络
I have an Image, I\'d like to replace all the pixels of one color with those in a different color, what is the simplest way to go about that?

I have an Image, I'd like to replace all the pixels of one color with those in a different color, what is the simplest way to go about that?

Mo开发者_如何学编程re or less I have an image in tkinter, and when a button is pressed I want the color to change.


try this.

#!/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.

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)

you can use color picker in gimp to absorb the color and see that's rgba color


I think that the fastest way to do that is to use the Image.load() method. Something like this should work:

from PIL import Image
im = Image.open("image.jpg")
image_data = im.load()
# Here you have access to the RGB color of each pixel
# image_data[x,y] = (R,G,B)
0

精彩评论

暂无评论...
验证码 换一张
取 消