ImageEnhance.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 __future__ import annotations
  21. from . import Image, ImageFilter, ImageStat
  22. class _Enhance:
  23. def enhance(self, factor):
  24. """
  25. Returns an enhanced image.
  26. :param factor: A floating point value controlling the enhancement.
  27. Factor 1.0 always returns a copy of the original image,
  28. lower factors mean less color (brightness, contrast,
  29. etc), and higher values more. There are no restrictions
  30. on this value.
  31. :rtype: :py:class:`~PIL.Image.Image`
  32. """
  33. return Image.blend(self.degenerate, self.image, factor)
  34. class Color(_Enhance):
  35. """Adjust image color balance.
  36. This class can be used to adjust the colour balance of an image, in
  37. a manner similar to the controls on a colour TV set. An enhancement
  38. factor of 0.0 gives a black and white image. A factor of 1.0 gives
  39. the original image.
  40. """
  41. def __init__(self, image):
  42. self.image = image
  43. self.intermediate_mode = "L"
  44. if "A" in image.getbands():
  45. self.intermediate_mode = "LA"
  46. self.degenerate = image.convert(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 gray 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"))