codekingpro/portable-devtools
115k
1#
2# The Python Imaging Library.
3# $Id$
4#
5# image enhancement classes
6#
7# For a background, see "Image Processing By Interpolation and
8# Extrapolation", Paul Haeberli and Douglas Voorhies. Available
9# at http://www.graficaobscura.com/interp/index.html
10#
11# History:
12# 1996-03-23 fl Created
13# 2009-06-16 fl Fixed mean calculation
14#
15# Copyright (c) Secret Labs AB 1997.
16# Copyright (c) Fredrik Lundh 1996.
17#
18# See the README file for information on usage and redistribution.
19#
20from __future__ import annotations
21
22from . import Image, ImageFilter, ImageStat
23
24
25class _Enhance:
26 image: Image.Image
27 degenerate: Image.Image
28
29 def enhance(self, factor: float) -> Image.Image:
30 """
31 Returns an enhanced image.
32
33 :param factor: A floating point value controlling the enhancement.
34 Factor 1.0 always returns a copy of the original image,
35 lower factors mean less color (brightness, contrast,
36 etc), and higher values more. There are no restrictions
37 on this value.
38 :rtype: :py:class:`~PIL.Image.Image`
39 """
40 return Image.blend(self.degenerate, self.image, factor)
41
42
43class Color(_Enhance):
44 """Adjust image color balance.
45
46 This class can be used to adjust the colour balance of an image, in
47 a manner similar to the controls on a colour TV set. An enhancement
48 factor of 0.0 gives a black and white image. A factor of 1.0 gives
49 the original image.
50 """
51
52 def __init__(self, image: Image.Image) -> None:
53 self.image = image
54 self.intermediate_mode = "L"
55 if "A" in image.getbands():
56 self.intermediate_mode = "LA"
57
58 if self.intermediate_mode != image.mode:
59 image = image.convert(self.intermediate_mode).convert(image.mode)
60 self.degenerate = image
61
62
63class Contrast(_Enhance):
64 """Adjust image contrast.
65
66 This class can be used to control the contrast of an image, similar
67 to the contrast control on a TV set. An enhancement factor of 0.0
68 gives a solid gray image. A factor of 1.0 gives the original image.
69 """
70
71 def __init__(self, image: Image.Image) -> None:
72 self.image = image
73 if image.mode != "L":
74 image = image.convert("L")
75 mean = int(ImageStat.Stat(image).mean[0] + 0.5)
76 self.degenerate = Image.new("L", image.size, mean)
77 if self.degenerate.mode != self.image.mode:
78 self.degenerate = self.degenerate.convert(self.image.mode)
79
80 if "A" in self.image.getbands():
81 self.degenerate.putalpha(self.image.getchannel("A"))
82
83
84class Brightness(_Enhance):
85 """Adjust image brightness.
86
87 This class can be used to control the brightness of an image. An
88 enhancement factor of 0.0 gives a black image. A factor of 1.0 gives the
89 original image.
90 """
91
92 def __init__(self, image: Image.Image) -> None:
93 self.image = image
94 self.degenerate = Image.new(image.mode, image.size, 0)
95
96 if "A" in image.getbands():
97 self.degenerate.putalpha(image.getchannel("A"))
98
99
100class Sharpness(_Enhance):
101 """Adjust image sharpness.
102
103 This class can be used to adjust the sharpness of an image. An
104 enhancement factor of 0.0 gives a blurred image, a factor of 1.0 gives the
105 original image, and a factor of 2.0 gives a sharpened image.
106 """
107
108 def __init__(self, image: Image.Image) -> None:
109 self.image = image
110 self.degenerate = image.filter(ImageFilter.SMOOTH)
111
112 if "A" in image.getbands():
113 self.degenerate.putalpha(image.getchannel("A"))
114 