ImageEnhance Module

Note

All source images in this document are derived from https://www.pexels.com (CC0 License).

Interface and class _Enhance

doc:http://pillow.readthedocs.io/en/4.1.x/reference/ImageEnhance.html#PIL.ImageEnhance._Enhance, http://effbot.org/imagingbook/imageenhance.htm#interface

All enhancement classes implement a common interface, containing a single method enhance(factor) which returns an enhanced image. So, you can always use enhancers in the same way:

from PIL import ImageEnhance

enhancer = ImageEnhance.Color(image)
#enhancer = ImageEnhance.Sharpness(image)

# Both Color and Sharpness has same 'enhance(factor)' method.
factor = 1.0
enhancer.enhance(factor).show("Sharpness %f" % factor)

The factor is a floating point value controlling the enhancement. Factor 1.0 always returns a copy of the original image, lower factors mean less color (brightness, contrast, etc), and higher values more. There are no restrictions on this value.

Actually, enhancement classes Color, Contrast, Brightness, and Sharpness are derived from class _Enhance:

PIL/ImageEnhance.py
from PIL import Image, ImageFilter, ImageStat

class _Enhance(object):

    def enhance(self, factor):
        return Image.blend(self.degenerate, self.image, factor)

Those derived enhancement classes set self.degenerate and self.image, like this:

PIL/ImageEnhance.py
class Color(_Enhance):
    def __init__(self, image):
        self.image = image
        self.intermediate_mode = 'L'
        if 'A' in image.getbands():
            self.intermediate_mode = 'LA'

        self.degenerate = image.convert(self.intermediate_mode).convert(image.mode)

class _Enhance is just implementation details (or only for implementer of custom enhancer), but official documents of Pillow (or PIL) mentions this internal class explicitly and imcompletely, so ignore those documents if you just want to use enhancers.

Predefined Enhancer classes

class Color

doc:http://pillow.readthedocs.io/en/4.1.x/reference/ImageEnhance.html#PIL.ImageEnhance.Color, http://effbot.org/imagingbook/imageenhance.htm#tag-ImageEnhance.Color
from PIL import Image, ImageEnhance

img = Image.open('data/srcimg06.jpg')
#
enhancer = ImageEnhance.Color(img)
enhancer.enhance(0.0).save(
    "result/ImageEnhance_Color_000.jpg")
enhancer.enhance(0.25).save(
    "result/ImageEnhance_Color_025.jpg")
enhancer.enhance(0.5).save(
    "result/ImageEnhance_Color_050.jpg")
enhancer.enhance(0.75).save(
    "result/ImageEnhance_Color_075.jpg")
enhancer.enhance(1.0).save(
    "result/ImageEnhance_Color_100.jpg")
factor=0.0 ImageEnhance_Color.res1 factor=0.25 ImageEnhance_Color.res2 factor=0.5 ImageEnhance_Color.res3 factor=0.75 ImageEnhance_Color.res4 factor=1.0 ImageEnhance_Color.res5

class Contrast

doc:http://pillow.readthedocs.io/en/4.1.x/reference/ImageEnhance.html#PIL.ImageEnhance.Contrast, http://effbot.org/imagingbook/imageenhance.htm#tag-ImageEnhance.Contrast
from PIL import Image, ImageEnhance

img = Image.open('data/srcimg21.jpg')
#
enhancer = ImageEnhance.Contrast(img)
enhancer.enhance(0.0).save(
    "result/ImageEnhance_Contrast_000.jpg")
enhancer.enhance(0.25).save(
    "result/ImageEnhance_Contrast_025.jpg")
enhancer.enhance(0.5).save(
    "result/ImageEnhance_Contrast_050.jpg")
enhancer.enhance(0.75).save(
    "result/ImageEnhance_Contrast_075.jpg")
enhancer.enhance(1.0).save(
    "result/ImageEnhance_Contrast_100.jpg")
factor=0.0 ImageEnhance_Contrast.res1 factor=0.25 ImageEnhance_Contrast.res2 factor=0.5 ImageEnhance_Contrast.res3 factor=0.75 ImageEnhance_Contrast.res4 factor=1.0 ImageEnhance_Contrast.res5

class Brightness

doc:http://pillow.readthedocs.io/en/4.1.x/reference/ImageEnhance.html#PIL.ImageEnhance.Brightness, http://effbot.org/imagingbook/imageenhance.htm#tag-ImageEnhance.Brightness
from PIL import Image, ImageEnhance

img = Image.open('data/srcimg22.jpg')
#
enhancer = ImageEnhance.Brightness(img)
enhancer.enhance(0.0).save(
    "result/ImageEnhance_Brightness_000.jpg")
enhancer.enhance(0.25).save(
    "result/ImageEnhance_Brightness_025.jpg")
enhancer.enhance(0.5).save(
    "result/ImageEnhance_Brightness_050.jpg")
enhancer.enhance(0.75).save(
    "result/ImageEnhance_Brightness_075.jpg")
enhancer.enhance(1.0).save(
    "result/ImageEnhance_Brightness_100.jpg")
factor=0.0 ImageEnhance_Brightness.res1 factor=0.25 ImageEnhance_Brightness.res2 factor=0.5 ImageEnhance_Brightness.res3 factor=0.75 ImageEnhance_Brightness.res4 factor=1.0 ImageEnhance_Brightness.res5

class Sharpness

doc:http://pillow.readthedocs.io/en/4.1.x/reference/ImageEnhance.html#PIL.ImageEnhance.Sharpness, http://effbot.org/imagingbook/imageenhance.htm#tag-ImageEnhance.Sharpness
from PIL import Image, ImageEnhance

img = Image.open('data/srcimg05.jpg')
#
enhancer = ImageEnhance.Sharpness(img)
enhancer.enhance(0.0).save(
    "result/ImageEnhance_Sharpness_000.jpg")
enhancer.enhance(0.5).save(
    "result/ImageEnhance_Sharpness_050.jpg")
enhancer.enhance(1.0).save(
    "result/ImageEnhance_Sharpness_100.jpg")
enhancer.enhance(1.5).save(
    "result/ImageEnhance_Sharpness_150.jpg")
enhancer.enhance(2.0).save(
    "result/ImageEnhance_Sharpness_200.jpg")
factor=0.0 ImageEnhance_Sharpness.res1 factor=0.5 ImageEnhance_Sharpness.res2 factor=1.0 ImageEnhance_Sharpness.res3 factor=1.5 ImageEnhance_Sharpness.res4 factor=2.0 ImageEnhance_Sharpness.res5