Search code examples
pythonimage-processingrgbgrayscale

Smooth conversion between color image to greyscale


I would like to smoothly convert an RGB image to greyscale as a function of some continuous parameter. I have seen plenty of posts on how to convert 3-channel to 1-channel, but that would not work for me, I would like the output to still be 3-channels. Is this possible?

I would like to have a function

f(image, parameter)

that does more or less the following: if paramater is zero, the function returns the original image, and if the parameter is one it returns a greyscale image. Therefore, I would have the ability to smoothly tune the color between on and off via parameter.

If there already is a coded solution, in Python is strongly preferred. Thanks!


Solution

  • It's quite easy to do with 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)