【问题标题】:Replacing a single color in PIL?替换 PIL 中的单一颜色?
【发布时间】:2023-03-16 20:08:01
【问题描述】:

我有一个图像,我想用另一种颜色的像素替换一种颜色的所有像素,最简单的方法是什么?

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

【问题讨论】:

标签: python python-imaging-library tkinter


【解决方案1】:

试试这个。

#!/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 颜色

【讨论】:

    【解决方案2】:

    我认为最快的方法是使用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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 1970-01-01
      • 2013-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多