将图像文件中的特定 RGB 颜色像素更改为另一种颜色

发布于 2024-11-17 08:29:59 字数 170 浏览 3 评论 0原文

我想用Python改变单一颜色。

如果存在 PIL 的快速解决方案,我会更喜欢这个解决方案。

目前,我使用

convert -background black -opaque '#939393' MyImage.png MyImage.png

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

convert -background black -opaque '#939393' MyImage.png MyImage.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

写给空气的情书 2024-11-24 08:29:59

如果您的计算机上可以使用 numpy,请尝试执行以下操作:

import numpy as np
from PIL import Image

im = Image.open('fig1.png')
data = np.array(im)

r1, g1, b1 = 0, 0, 0 # Original value
r2, g2, b2 = 255, 255, 255 # Value that we want to replace it with

red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:3][mask] = [r2, g2, b2]

im = Image.fromarray(data)
im.save('fig1_modified.png')

它将使用更多 (3 倍) 的内存,但速度应该要快得多(约 5 倍,但对于较大的图像,速度会更快)。

另请注意,如果您只有 RGB(而不是 RGBA)图像,上面的代码会比实际需要的代码稍微复杂一些。然而,这个例子将单独保留 alpha 波段,而更简单的版本则不会。

If numpy is available on your machine, try doing something like:

import numpy as np
from PIL import Image

im = Image.open('fig1.png')
data = np.array(im)

r1, g1, b1 = 0, 0, 0 # Original value
r2, g2, b2 = 255, 255, 255 # Value that we want to replace it with

red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:3][mask] = [r2, g2, b2]

im = Image.fromarray(data)
im.save('fig1_modified.png')

It will use a bit (3x) more memory, but it should be considerably (~5x, but more for bigger images) faster.

Also note that the code above is slightly more complicated than it needs to be if you only have RGB (and not RGBA) images. However, this example will leave the alpha band alone, whereas a simpler version wouldn't have.

不即不离 2024-11-24 08:29:59

这是对乔·金顿上述答案的修改。如果您的图像也包含 Alpha 通道,以下是如何执行此操作。

import numpy as np
import Image

im = Image.open('fig1.png')
im = im.convert('RGBA')
data = np.array(im)

r1, g1, b1 = 0, 0, 0 # Original value
r2, g2, b2, a2 = 255, 255, 255, 255 # Value that we want to replace it with

red, green, blue, alpha = data[:,:,0], data[:,:,1], data[:,:,2], data[:,:,3]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:4][mask] = [r2, g2, b2, a2]

im = Image.fromarray(data)
im.save('fig1_modified.png')

我花了很长时间才弄清楚如何让它发挥作用。我希望它对其他人有帮助。

This is a modification of Joe Kington's answer above. The following is how to do this if your image contains an alpha channel as well.

import numpy as np
import Image

im = Image.open('fig1.png')
im = im.convert('RGBA')
data = np.array(im)

r1, g1, b1 = 0, 0, 0 # Original value
r2, g2, b2, a2 = 255, 255, 255, 255 # Value that we want to replace it with

red, green, blue, alpha = data[:,:,0], data[:,:,1], data[:,:,2], data[:,:,3]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:4][mask] = [r2, g2, b2, a2]

im = Image.fromarray(data)
im.save('fig1_modified.png')

It took me a long time to figure out how to get it to work. I hope that it helps someone else.

神经暖 2024-11-24 08:29:59

我刚刚想出了这个解决方案:

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')

虽然 putpixel 速度不快,但对我来说似乎足够快了。

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.

一生独一 2024-11-24 08:29:59

此解决方案使用 glob 编辑文件夹中的所有 png,删除一种颜色并将其替换为另一种颜色,但使用 RGBA。

import glob
from PIL import Image

old_color = 255, 0, 255, 255
new_color = 0, 0, 0, 0

for path in glob.glob("*.png"):
    if "__out" in path:
        print "skipping on", path
        continue

    print "working on", path

    im = Image.open(path)
    im = im.convert("RGBA")
    width, height = im.size
    colortuples = im.getcolors()

    pix = im.load()
    for x in xrange(0, width):
        for y in xrange(0, height):
            if pix[x,y] == old_color:
                im.putpixel((x, y), new_color)

    im.save(path+"__out.png")

这是 https://stackoverflow.com/a/6483549/541208 的修改

This solution uses glob to edit all pngs in a folder, removing a color and swapping it out with another, but uses RGBA.

import glob
from PIL import Image

old_color = 255, 0, 255, 255
new_color = 0, 0, 0, 0

for path in glob.glob("*.png"):
    if "__out" in path:
        print "skipping on", path
        continue

    print "working on", path

    im = Image.open(path)
    im = im.convert("RGBA")
    width, height = im.size
    colortuples = im.getcolors()

    pix = im.load()
    for x in xrange(0, width):
        for y in xrange(0, height):
            if pix[x,y] == old_color:
                im.putpixel((x, y), new_color)

    im.save(path+"__out.png")

It's a modification of https://stackoverflow.com/a/6483549/541208

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文