2
# The Python Imaging Library.
5
# image enhancement classes
7
# For a background, see "Image Processing By Interpolation and
8
# Extrapolation", Paul Haeberli and Douglas Voorhies. Available
9
# at http://www.sgi.com/grafica/interp/index.html
12
# 1996-03-23 fl Created
13
# 2009-06-16 fl Fixed mean calculation
15
# Copyright (c) Secret Labs AB 1997.
16
# Copyright (c) Fredrik Lundh 1996.
18
# See the README file for information on usage and redistribution.
21
import Image, ImageFilter, ImageStat
26
# Returns an enhanced image. The enhancement factor is a floating
27
# point value controlling the enhancement. Factor 1.0 always
28
# returns a copy of the original image, lower factors mean less
29
# colour (brightness, contrast, etc), and higher values more.
30
# There are no restrictions on this value.
32
# @param factor Enhancement factor.
33
# @return An enhanced image.
35
def enhance(self, factor):
36
return Image.blend(self.degenerate, self.image, factor)
39
# Color enhancement object.
41
# This class can be used to adjust the colour balance of an image, in
42
# a manner similar to the controls on a colour TV set. An enhancement
43
# factor of 0.0 gives a black and white image, a factor of 1.0 gives
46
class Color(_Enhance):
47
"Adjust image colour balance"
48
def __init__(self, image):
50
self.degenerate = image.convert("L").convert(image.mode)
53
# Contrast enhancement object.
55
# This class can be used to control the contrast of an image, similar
56
# to the contrast control on a TV set. An enhancement factor of 0.0
57
# gives a solid grey image, factor 1.0 gives the original image.
59
class Contrast(_Enhance):
60
"Adjust image contrast"
61
def __init__(self, image):
63
mean = int(ImageStat.Stat(image.convert("L")).mean[0] + 0.5)
64
self.degenerate = Image.new("L", image.size, mean).convert(image.mode)
67
# Brightness enhancement object.
69
# This class can be used to control the brighntess of an image. An
70
# enhancement factor of 0.0 gives a black image, factor 1.0 gives the
73
class Brightness(_Enhance):
74
"Adjust image brightness"
75
def __init__(self, image):
77
self.degenerate = Image.new(image.mode, image.size, 0)
80
# Sharpness enhancement object.
82
# This class can be used to adjust the sharpness of an image. The
83
# enhancement factor 0.0 gives a blurred image, 1.0 gives the original
84
# image, and a factor of 2.0 gives a sharpened image.
86
class Sharpness(_Enhance):
87
"Adjust image sharpness"
88
def __init__(self, image):
90
self.degenerate = image.filter(ImageFilter.SMOOTH)