Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change saturation with Imagekit, PIL or Pillow?

How do I go about changing the saturation of an image using PIL or Pillow? Preferably I'd like to be able to use the solution together with the django-imagekit package. The reason I need to change the saturation is to create an effect where when the user hovers a black-and-white image it turns to colored.

like image 945
antonagestam Avatar asked Apr 17 '13 20:04

antonagestam


People also ask

What is pillow in image processing?

Pillow is a fork of the Python Imaging Library (PIL). PIL is a library that offers several standard procedures for manipulating images. It's a powerful library but hasn't been updated since 2009 and doesn't support Python 3. Pillow builds on this, adding more features and support for Python 3.

How do you change the image Hue in Python?

The image hue is adjusted by converting the image to HSV (Hue, Saturation, Value) and cyclically shifting the intensities in the hue channel (H). The image is then converted back to original image mode.

How do you invert an image in a python pillow?

ImageOps. flip()、ImageOps. mirror() The ImageOps module of the Python image processing library Pillow(PIL) provides flip() to flip the image upside down (vertically) and mirror() to flip the left and right (horizontally).


1 Answers

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 isn't exactly the same thing as half the saturation (because half or double the saturation would usually underflow or overflow), but it's probably what you actually want most of the time. As the docs say, it works like the "color" knob on a TV.

Here's an example of the same image at 0.5, 1.0, and 2.0 color: enter image description hereenter image description hereenter image description here

like image 135
abarnert Avatar answered Sep 19 '22 12:09

abarnert