【发布时间】:2011-07-02 06:48:01
【问题描述】:
我希望每个不是黑色的像素都设置为白色(或任意颜色)。
我在 Python 中需要这个(最好使用 PIL,但也可以考虑其他库)
谢谢
【问题讨论】:
-
你点击了“关闭”链接,为什么不向我解释一下你为什么这样做?
标签: python image-processing python-imaging-library
我希望每个不是黑色的像素都设置为白色(或任意颜色)。
我在 Python 中需要这个(最好使用 PIL,但也可以考虑其他库)
谢谢
【问题讨论】:
标签: python image-processing python-imaging-library
试试这个:
import sys
from PIL import Image
imin = Image.open(sys.argv[1])
imout = Image.new("RGB", imin.size)
imout.putdata(map(
lambda pixel: (0,0,0) if pixel == (0,0,0) else (255,255,255),
imin.getdata()
)
)
imout.save(sys.argv[2])
【讨论】:
尝试使用Image.blend()。假设你的图片是im。
# conversion matrix: any color to white, black to black
mtx = (1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0)
mask = im.convert("L", mtx) # show() it to get the idea
decal = Image.new("RGB", im.size, (0, 0, 255)) # we fill with blue
Image.blend(im, decal, mask).show() # all black turned blue
这必须比每像素 lambda 调用快得多,尤其是在大图像上。
【讨论】:
使用 PIL
c = color_of_choice
out = im.point(lambda i: c if i>0 else i)
【讨论】: