ImageEnhance.py 3.5 KB

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