ImageEnhance.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. """
  34. return Image.blend(self.degenerate, self.image, factor)
  35. class Color(_Enhance):
  36. """Adjust image color balance.
  37. This class can be used to adjust the colour balance of an image, in
  38. a manner similar to the controls on a colour TV set. An enhancement
  39. factor of 0.0 gives a black and white image. A factor of 1.0 gives
  40. the original image.
  41. """
  42. def __init__(self, image: Image.Image) -> None:
  43. self.image = image
  44. self.intermediate_mode = "L"
  45. if "A" in image.getbands():
  46. self.intermediate_mode = "LA"
  47. if self.intermediate_mode != image.mode:
  48. image = image.convert(self.intermediate_mode).convert(image.mode)
  49. self.degenerate = image
  50. class Contrast(_Enhance):
  51. """Adjust image contrast.
  52. This class can be used to control the contrast of an image, similar
  53. to the contrast control on a TV set. An enhancement factor of 0.0
  54. gives a solid gray image. A factor of 1.0 gives the original image.
  55. """
  56. def __init__(self, image: Image.Image) -> None:
  57. self.image = image
  58. if image.mode != "L":
  59. image = image.convert("L")
  60. mean = int(ImageStat.Stat(image).mean[0] + 0.5)
  61. self.degenerate = Image.new("L", image.size, mean)
  62. if self.degenerate.mode != self.image.mode:
  63. self.degenerate = self.degenerate.convert(self.image.mode)
  64. if "A" in self.image.getbands():
  65. self.degenerate.putalpha(self.image.getchannel("A"))
  66. class Brightness(_Enhance):
  67. """Adjust image brightness.
  68. This class can be used to control the brightness of an image. An
  69. enhancement factor of 0.0 gives a black image. A factor of 1.0 gives the
  70. original image.
  71. """
  72. def __init__(self, image: Image.Image) -> None:
  73. self.image = image
  74. self.degenerate = Image.new(image.mode, image.size, 0)
  75. if "A" in image.getbands():
  76. self.degenerate.putalpha(image.getchannel("A"))
  77. class Sharpness(_Enhance):
  78. """Adjust image sharpness.
  79. This class can be used to adjust the sharpness of an image. An
  80. enhancement factor of 0.0 gives a blurred image, a factor of 1.0 gives the
  81. original image, and a factor of 2.0 gives a sharpened image.
  82. """
  83. def __init__(self, image: Image.Image) -> None:
  84. self.image = image
  85. self.degenerate = image.filter(ImageFilter.SMOOTH)
  86. if "A" in image.getbands():
  87. self.degenerate.putalpha(image.getchannel("A"))