Home / Library / PIL / Handbook / The ImageEnhance module

The ImageEnhance module

Every enhancer works the same way. Make one from an image, then call enhance with a factor.

from PIL import Image, ImageEnhance

im = Image.open("photo.jpg")
brighter = ImageEnhance.Brightness(im).enhance(1.3)

The factor

1.0 always returns the original. Below 1.0 moves towards the degenerate version of that property, above 1.0 moves away from it.

Enhancer0.0 gives youAbove 1.0
BrightnessA black imageBrighter
ContrastA flat grey image at the average brightnessMore contrast
ColorGreyscaleMore saturated
SharpnessBlurredSharpened

That is the whole idea: interpolate between a degenerate image and the original, and keep going past it. Values are clipped at the ends of the range, so you cannot push a channel below 0 or above 255.

A usable amount

im = ImageEnhance.Color(im).enhance(1.15)
im = ImageEnhance.Contrast(im).enhance(1.1)
im = ImageEnhance.Sharpness(im).enhance(1.4)

Small numbers do more than people expect. Anything past about 1.5 on colour starts looking like a phone filter. Apply enhancements before you resize down, not after, because resizing averages away some of what you just did.

Enhance or filter

ImageEnhance.Sharpness is a simple blend towards or away from a blurred copy. For real sharpening, particularly after resizing, ImageFilter.UnsharpMask gives you radius and threshold as well as strength, and does a better job. See the filter module.

The playground has sliders for brightness and contrast and writes the matching code.

Related