ImageChops.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # standard channel operations
  6. #
  7. # History:
  8. # 1996-03-24 fl Created
  9. # 1996-08-13 fl Added logical operations (for "1" images)
  10. # 2000-10-12 fl Added offset method (from Image.py)
  11. #
  12. # Copyright (c) 1997-2000 by Secret Labs AB
  13. # Copyright (c) 1996-2000 by Fredrik Lundh
  14. #
  15. # See the README file for information on usage and redistribution.
  16. #
  17. from . import Image
  18. def constant(image, value):
  19. """Fill a channel with a given grey level.
  20. :rtype: :py:class:`~PIL.Image.Image`
  21. """
  22. return Image.new("L", image.size, value)
  23. def duplicate(image):
  24. """Copy a channel. Alias for :py:meth:`PIL.Image.Image.copy`.
  25. :rtype: :py:class:`~PIL.Image.Image`
  26. """
  27. return image.copy()
  28. def invert(image):
  29. """
  30. Invert an image (channel). ::
  31. out = MAX - image
  32. :rtype: :py:class:`~PIL.Image.Image`
  33. """
  34. image.load()
  35. return image._new(image.im.chop_invert())
  36. def lighter(image1, image2):
  37. """
  38. Compares the two images, pixel by pixel, and returns a new image containing
  39. the lighter values. ::
  40. out = max(image1, image2)
  41. :rtype: :py:class:`~PIL.Image.Image`
  42. """
  43. image1.load()
  44. image2.load()
  45. return image1._new(image1.im.chop_lighter(image2.im))
  46. def darker(image1, image2):
  47. """
  48. Compares the two images, pixel by pixel, and returns a new image containing
  49. the darker values. ::
  50. out = min(image1, image2)
  51. :rtype: :py:class:`~PIL.Image.Image`
  52. """
  53. image1.load()
  54. image2.load()
  55. return image1._new(image1.im.chop_darker(image2.im))
  56. def difference(image1, image2):
  57. """
  58. Returns the absolute value of the pixel-by-pixel difference between the two
  59. images. ::
  60. out = abs(image1 - image2)
  61. :rtype: :py:class:`~PIL.Image.Image`
  62. """
  63. image1.load()
  64. image2.load()
  65. return image1._new(image1.im.chop_difference(image2.im))
  66. def multiply(image1, image2):
  67. """
  68. Superimposes two images on top of each other.
  69. If you multiply an image with a solid black image, the result is black. If
  70. you multiply with a solid white image, the image is unaffected. ::
  71. out = image1 * image2 / MAX
  72. :rtype: :py:class:`~PIL.Image.Image`
  73. """
  74. image1.load()
  75. image2.load()
  76. return image1._new(image1.im.chop_multiply(image2.im))
  77. def screen(image1, image2):
  78. """
  79. Superimposes two inverted images on top of each other. ::
  80. out = MAX - ((MAX - image1) * (MAX - image2) / MAX)
  81. :rtype: :py:class:`~PIL.Image.Image`
  82. """
  83. image1.load()
  84. image2.load()
  85. return image1._new(image1.im.chop_screen(image2.im))
  86. def soft_light(image1, image2):
  87. """
  88. Superimposes two images on top of each other using the Soft Light algorithm
  89. :rtype: :py:class:`~PIL.Image.Image`
  90. """
  91. image1.load()
  92. image2.load()
  93. return image1._new(image1.im.chop_soft_light(image2.im))
  94. def hard_light(image1, image2):
  95. """
  96. Superimposes two images on top of each other using the Hard Light algorithm
  97. :rtype: :py:class:`~PIL.Image.Image`
  98. """
  99. image1.load()
  100. image2.load()
  101. return image1._new(image1.im.chop_hard_light(image2.im))
  102. def overlay(image1, image2):
  103. """
  104. Superimposes two images on top of each other using the Overlay algorithm
  105. :rtype: :py:class:`~PIL.Image.Image`
  106. """
  107. image1.load()
  108. image2.load()
  109. return image1._new(image1.im.chop_overlay(image2.im))
  110. def add(image1, image2, scale=1.0, offset=0):
  111. """
  112. Adds two images, dividing the result by scale and adding the
  113. offset. If omitted, scale defaults to 1.0, and offset to 0.0. ::
  114. out = ((image1 + image2) / scale + offset)
  115. :rtype: :py:class:`~PIL.Image.Image`
  116. """
  117. image1.load()
  118. image2.load()
  119. return image1._new(image1.im.chop_add(image2.im, scale, offset))
  120. def subtract(image1, image2, scale=1.0, offset=0):
  121. """
  122. Subtracts two images, dividing the result by scale and adding the offset.
  123. If omitted, scale defaults to 1.0, and offset to 0.0. ::
  124. out = ((image1 - image2) / scale + offset)
  125. :rtype: :py:class:`~PIL.Image.Image`
  126. """
  127. image1.load()
  128. image2.load()
  129. return image1._new(image1.im.chop_subtract(image2.im, scale, offset))
  130. def add_modulo(image1, image2):
  131. """Add two images, without clipping the result. ::
  132. out = ((image1 + image2) % MAX)
  133. :rtype: :py:class:`~PIL.Image.Image`
  134. """
  135. image1.load()
  136. image2.load()
  137. return image1._new(image1.im.chop_add_modulo(image2.im))
  138. def subtract_modulo(image1, image2):
  139. """Subtract two images, without clipping the result. ::
  140. out = ((image1 - image2) % MAX)
  141. :rtype: :py:class:`~PIL.Image.Image`
  142. """
  143. image1.load()
  144. image2.load()
  145. return image1._new(image1.im.chop_subtract_modulo(image2.im))
  146. def logical_and(image1, image2):
  147. """Logical AND between two images.
  148. Both of the images must have mode "1". If you would like to perform a
  149. logical AND on an image with a mode other than "1", try
  150. :py:meth:`~PIL.ImageChops.multiply` instead, using a black-and-white mask
  151. as the second image. ::
  152. out = ((image1 and image2) % MAX)
  153. :rtype: :py:class:`~PIL.Image.Image`
  154. """
  155. image1.load()
  156. image2.load()
  157. return image1._new(image1.im.chop_and(image2.im))
  158. def logical_or(image1, image2):
  159. """Logical OR between two images.
  160. Both of the images must have mode "1". ::
  161. out = ((image1 or image2) % MAX)
  162. :rtype: :py:class:`~PIL.Image.Image`
  163. """
  164. image1.load()
  165. image2.load()
  166. return image1._new(image1.im.chop_or(image2.im))
  167. def logical_xor(image1, image2):
  168. """Logical XOR between two images.
  169. Both of the images must have mode "1". ::
  170. out = ((bool(image1) != bool(image2)) % MAX)
  171. :rtype: :py:class:`~PIL.Image.Image`
  172. """
  173. image1.load()
  174. image2.load()
  175. return image1._new(image1.im.chop_xor(image2.im))
  176. def blend(image1, image2, alpha):
  177. """Blend images using constant transparency weight. Alias for
  178. :py:func:`PIL.Image.blend`.
  179. :rtype: :py:class:`~PIL.Image.Image`
  180. """
  181. return Image.blend(image1, image2, alpha)
  182. def composite(image1, image2, mask):
  183. """Create composite using transparency mask. Alias for
  184. :py:func:`PIL.Image.composite`.
  185. :rtype: :py:class:`~PIL.Image.Image`
  186. """
  187. return Image.composite(image1, image2, mask)
  188. def offset(image, xoffset, yoffset=None):
  189. """Returns a copy of the image where data has been offset by the given
  190. distances. Data wraps around the edges. If ``yoffset`` is omitted, it
  191. is assumed to be equal to ``xoffset``.
  192. :param image: Input image.
  193. :param xoffset: The horizontal distance.
  194. :param yoffset: The vertical distance. If omitted, both
  195. distances are set to the same value.
  196. :rtype: :py:class:`~PIL.Image.Image`
  197. """
  198. if yoffset is None:
  199. yoffset = xoffset
  200. image.load()
  201. return image._new(image.im.offset(xoffset, yoffset))