ImageEnhance.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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(self.intermediate_mode).convert(image.mode)
  46. class Contrast(_Enhance):
  47. """Adjust image contrast.
  48. This class can be used to control the contrast of an image, similar
  49. to the contrast control on a TV set. An enhancement factor of 0.0
  50. gives a solid grey image. A factor of 1.0 gives the original image.
  51. """
  52. def __init__(self, image):
  53. self.image = image
  54. mean = int(ImageStat.Stat(image.convert("L")).mean[0] + 0.5)
  55. self.degenerate = Image.new("L", image.size, mean).convert(image.mode)
  56. if "A" in image.getbands():
  57. self.degenerate.putalpha(image.getchannel("A"))
  58. class Brightness(_Enhance):
  59. """Adjust image brightness.
  60. This class can be used to control the brightness of an image. An
  61. enhancement factor of 0.0 gives a black image. A factor of 1.0 gives the
  62. original image.
  63. """
  64. def __init__(self, image):
  65. self.image = image
  66. self.degenerate = Image.new(image.mode, image.size, 0)
  67. if "A" in image.getbands():
  68. self.degenerate.putalpha(image.getchannel("A"))
  69. class Sharpness(_Enhance):
  70. """Adjust image sharpness.
  71. This class can be used to adjust the sharpness of an image. An
  72. enhancement factor of 0.0 gives a blurred image, a factor of 1.0 gives the
  73. original image, and a factor of 2.0 gives a sharpened image.
  74. """
  75. def __init__(self, image):
  76. self.image = image
  77. self.degenerate = image.filter(ImageFilter.SMOOTH)
  78. if "A" in image.getbands():
  79. self.degenerate.putalpha(image.getchannel("A"))