小编Ujw*_*til的帖子

自定义(更改)图像(像素)颜色 - python

我想自定义图像颜色以制作具有颜色变体的类似图像。

例子 :

标志_1 标志_2

对于上面的图像,我想用其他颜色替换红色,例如蓝色,绿色,黄色,黑色等。

我试过 :

from PIL import Image
filename ="./Logo.jpg"

picture = Image.open(filename, 'r')
_colors = [(255, 255, 255), (128, 128, 0), (128, 128, 128), (192, 128, 0), (128, 64, 0), (0, 192, 0), (128, 64, 128), (255, 255, 255)]
width, height = picture.size

for x in range(0, width):
    for y in range(0, height):
        current_color = picture.getpixel((x,y))
        # print (current_color)
        if current_color in _colors:
            picture.putpixel((x,y), (255,5, 255))
            # print ("Y")

picture.save("./test/change.png")

Run Code Online (Sandbox Code Playgroud)

上面的代码是非常常见的代码,建议大多数人使用。但这是相当困难的,因为它替换了列表“ _colors ”中的像素输出图像是:

错误的输出

上述问题有什么解决办法吗?有什么聪明的方法可以使用机器学习来处理这个问题吗?有使用其他编程语言的解决方案吗?

python pixel image-processing python-imaging-library

5
推荐指数
1
解决办法
2428
查看次数