You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.1 KiB

4 years ago
  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. #
  20. from . import Image, ImageFilter, ImageStat
  21. class _Enhance(object):
  22. def enhance(self, factor):
  23. """
  24. Returns an enhanced image.
  25. :param factor: A floating point value controlling the enhancement.
  26. Factor 1.0 always returns a copy of the original image,
  27. lower factors mean less color (brightness, contrast,
  28. etc), and higher values more. There are no restrictions
  29. on this value.
  30. :rtype: :py:class:`~PIL.Image.Image`
  31. """
  32. return Image.blend(self.degenerate, self.image, factor)
  33. class Color(_Enhance):
  34. """Adjust image color balance.
  35. This class can be used to adjust the colour balance of an image, in
  36. a manner similar to the controls on a colour TV set. An enhancement
  37. factor of 0.0 gives a black and white image. A factor of 1.0 gives
  38. the original image.
  39. """
  40. def __init__(self, image):
  41. self.image = image
  42. self.intermediate_mode = 'L'
  43. if 'A' in image.getbands():
  44. self.intermediate_mode = 'LA'
  45. self.degenerate = image.convert(
  46. self.intermediate_mode).convert(image.mode)
  47. class Contrast(_Enhance):
  48. """Adjust image contrast.
  49. This class can be used to control the contrast of an image, similar
  50. to the contrast control on a TV set. An enhancement factor of 0.0
  51. gives a solid grey image. A factor of 1.0 gives the original image.
  52. """
  53. def __init__(self, image):
  54. self.image = image
  55. mean = int(ImageStat.Stat(image.convert("L")).mean[0] + 0.5)
  56. self.degenerate = Image.new("L", image.size, mean).convert(image.mode)
  57. if 'A' in image.getbands():
  58. self.degenerate.putalpha(image.getchannel('A'))
  59. class Brightness(_Enhance):
  60. """Adjust image brightness.
  61. This class can be used to control the brightness of an image. An
  62. enhancement factor of 0.0 gives a black image. A factor of 1.0 gives the
  63. original image.
  64. """
  65. def __init__(self, image):
  66. self.image = image
  67. self.degenerate = Image.new(image.mode, image.size, 0)
  68. if 'A' in image.getbands():
  69. self.degenerate.putalpha(image.getchannel('A'))
  70. class Sharpness(_Enhance):
  71. """Adjust image sharpness.
  72. This class can be used to adjust the sharpness of an image. An
  73. enhancement factor of 0.0 gives a blurred image, a factor of 1.0 gives the
  74. original image, and a factor of 2.0 gives a sharpened image.
  75. """
  76. def __init__(self, image):
  77. self.image = image
  78. self.degenerate = image.filter(ImageFilter.SMOOTH)
  79. if 'A' in image.getbands():
  80. self.degenerate.putalpha(image.getchannel('A'))