Change saturation with Imagekit, PIL or Pillow?

How do I change the saturation of an image using PIL or Pillow? It is advisable that I can use this solution with the django-imagekit package. The reason I need to change the saturation is to create an effect when the user hovers a black and white image, it becomes color.

+4
source share
3 answers

If you are using django-imagekit, you can simply use the supplied Adjust processor:

 from imagekit.processors import Adjust Adjust(color=0.5) 

Under the hood, this will do exactly what @abarnert recommends .

+2
source

You probably want ImageEnhance.Color .

 img = PIL.Image.open('bus.png') converter = PIL.ImageEnhance.Color(img) img2 = converter.enhance(0.5) 

This gives an image with half the "color" of the original. This is not the same as half saturation (because half or double saturation usually ends or overflows), but it is probably what you really want most of the time. According to the documents, it works like a โ€œcolorโ€ pen on a TV.

Here is an example of the same image in colors 0.5, 1.0 and 2.0: enter image description hereenter image description hereenter image description here

+9
source

If you want a grayscale image, just convert it to L (Luminance) mode:

 greyscale = rgba_image.convert('L') 

Applying this to my ninja:

cute ninjagreyscale cute ninja

If you need intermediate steps, you need to convert the RGB value to HLS or HSV, adjust the saturation, and then convert it to RGB again. You can use colorsys for this or adapt this numpy solution ; I would expect the latter to work better.

+2
source

All Articles