更换PIL中的单色?

时间:2010-07-02 22:07:52

标签: python python-imaging-library tkinter

我有一张图片,我想将一种颜色的所有像素替换为不同颜色的像素,最简单的方法是什么?

或多或少我在tkinter中有一个图像,当按下一个按钮时我希望颜色改变。

3 个答案:

答案 0 :(得分:4)

试试这个。

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

你可以在gimp中使用颜色选择器来吸收颜色并看到它的rgba颜色

答案 1 :(得分:3)

我认为最快的方法是使用Image.load()方法。 这样的事情应该有效:

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)

答案 2 :(得分:0)

执行此操作的最佳方法是使用point方法。 请尝试使用此thread进行完整解释。