【问题标题】:How to change color of image using Python?如何使用 Python 更改图像的颜色?
【发布时间】:2015-09-15 05:27:26
【问题描述】:

我有一个小的灰色图像。我需要为此图像创建许多彩色副本(黄色一个,绿色一个,...)。

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

如何使用 Python 做到这一点?

【问题讨论】:

  • 您可以尝试PILPillow(友好的PIL fork)模块。这是文件:pillow.readthedocs.org
  • 添加更多信息。

标签: python


【解决方案1】:

我今天在Hacker News 上看到一篇文章,该文章展示了如何将恒定基色的图像与affine transform 混合。这篇文章是 William Chargin 的 Making thumbnails fast,它是关于提高图像处理性能的。其中提到的源代码在affine transforms on PIL images

这是一个演示,从灰度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

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

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

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

【讨论】:

  • 这太棒了。谢谢
【解决方案2】:

这可能有点矫枉过正,但您可以轻松使用 OpenCV 库(python 绑定)中的功能为灰度图像着色。

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

以下是推荐的行动方案:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 2020-11-03
    • 2020-10-10
    • 2020-09-20
    相关资源
    最近更新 更多