彩色图像与灰度图像之间的平滑转换

时间:2021-07-29 04:09:48

标签: python image-processing rgb grayscale

我想将 RGB 图像平滑地转换为灰度,作为某些连续 parameter 的函数。我看过很多关于如何将 3 通道转换为 1 通道的帖子,但这对我不起作用,我希望输出仍然是 3 通道。这可能吗?

我想要一个功能

f(image, parameter)

或多或少执行以下操作:如果 paramater 为零,则函数返回原始 image,如果 parameter 为一,则返回灰度图像。因此,我将能够通过 parameter 在打开和关闭之间平滑地调整颜色。

如果已经有编码解决方案,则强烈建议使用 Python。 谢谢!

1 个答案:

答案 0 :(得分:3)

使用 PIL/Pillow 很容易做到。

from PIL import Image
im = Image.open(r'c:\temp\temp.jpg')
gray = im.copy().convert('L').convert('RGB')
im2 = Image.blend(im, gray, 0.75)