如何使用Python更改图像的颜色?

8

我有一张小的灰度图像。我需要创建许多彩色副本(黄色,绿色等)。

我不需要替换单个颜色。我的原始图像包含许多灰度级别,我需要创建具有许多其他颜色阴影的图像。

如何使用Python完成这个任务?


你可以尝试使用PIL或者Pillow(一个友好的PIL分支)模块。这里是文档链接:https://pillow.readthedocs.org/ - Remi Guan
增加一些更多的信息。 - Kaleem Ullah
2个回答

15

今天我在Hacker News上看到了一篇文章,介绍了如何通过常量基色和仿射变换混合图像。这篇文章是由William Chargin撰写的快速制作缩略图,主要讲述了如何提高图像处理性能。其中提到的源代码可以在PIL图像上进行仿射变换中找到。

下面是一个演示,从一个灰度Lena图像开始,将其调整为231x231像素。选择这张图片是因为它是“自1973年以来在图像处理领域广泛使用的标准测试图像”。

from PIL import Image
from transforms import RGBTransform # from source code mentioned above

lena = Image.open("lena.png")
lena = lena.convert('RGB') # ensure image has 3 channels
lena

starting image

red = RGBTransform().mix_with((255, 0, 0),factor=.30).applied_to(lena)    
red

red tinted image

green = RGBTransform().mix_with((0, 255, 0),factor=.30).applied_to(lena)
green

green tinted image

blue = RGBTransform().mix_with((0, 0, 255),factor=.30).applied_to(lena)
blue

blue tinted image


这太棒了。谢谢。 - goodcow

1

这可能有些过度,但您可以轻松地使用OpenCV库(Python绑定)的功能将灰度图像着色。

尝试查看这些人的C ++代码:http://answers.opencv.org/question/50781/false-coloring-of-grayscale-image/。他们使用的函数的类似物可能存在于Python库中。

下面是建议采取的行动:

  1. 使用cv2.cvtColor()将图像从灰度转换为BGR(OpenCV约定以相反顺序列出红绿蓝)
  2. 应用您选择的人工色彩映射(cv2.applyColorMap()),请参见:http://docs.opencv.org/modules/contrib/doc/facerec/colormaps.html

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接