ImageChops.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. .. code-block:: python
  32. out = MAX - image
  33. :rtype: :py:class:`~PIL.Image.Image`
  34. """
  35. image.load()
  36. return image._new(image.im.chop_invert())
  37. def lighter(image1, image2):
  38. """
  39. Compares the two images, pixel by pixel, and returns a new image containing
  40. the lighter values.
  41. .. code-block:: python
  42. out = max(image1, image2)
  43. :rtype: :py:class:`~PIL.Image.Image`
  44. """
  45. image1.load()
  46. image2.load()
  47. return image1._new(image1.im.chop_lighter(image2.im))
  48. def darker(image1, image2):
  49. """
  50. Compares the two images, pixel by pixel, and returns a new image containing
  51. the darker values.
  52. .. code-block:: python
  53. out = min(image1, image2)
  54. :rtype: :py:class:`~PIL.Image.Image`
  55. """
  56. image1.load()
  57. image2.load()
  58. return image1._new(image1.im.chop_darker(image2.im))
  59. def difference(image1, image2):
  60. """
  61. Returns the absolute value of the pixel-by-pixel difference between the two
  62. images.
  63. .. code-block:: python
  64. out = abs(image1 - image2)
  65. :rtype: :py:class:`~PIL.Image.Image`
  66. """
  67. image1.load()
  68. image2.load()
  69. return image1._new(image1.im.chop_difference(image2.im))
  70. def multiply(image1, image2):
  71. """
  72. Superimposes two images on top of each other.
  73. If you multiply an image with a solid black image, the result is black. If
  74. you multiply with a solid white image, the image is unaffected.
  75. .. code-block:: python
  76. out = image1 * image2 / MAX
  77. :rtype: :py:class:`~PIL.Image.Image`
  78. """
  79. image1.load()
  80. image2.load()
  81. return image1._new(image1.im.chop_multiply(image2.im))
  82. def screen(image1, image2):
  83. """
  84. Superimposes two inverted images on top of each other.
  85. .. code-block:: python
  86. out = MAX - ((MAX - image1) * (MAX - image2) / MAX)
  87. :rtype: :py:class:`~PIL.Image.Image`
  88. """
  89. image1.load()
  90. image2.load()
  91. return image1._new(image1.im.chop_screen(image2.im))
  92. def soft_light(image1, image2):
  93. """
  94. Superimposes two images on top of each other using the Soft Light algorithm
  95. :rtype: :py:class:`~PIL.Image.Image`
  96. """
  97. image1.load()
  98. image2.load()
  99. return image1._new(image1.im.chop_soft_light(image2.im))
  100. def hard_light(image1, image2):
  101. """
  102. Superimposes two images on top of each other using the Hard Light algorithm
  103. :rtype: :py:class:`~PIL.Image.Image`
  104. """
  105. image1.load()
  106. image2.load()
  107. return image1._new(image1.im.chop_hard_light(image2.im))
  108. def overlay(image1, image2):
  109. """
  110. Superimposes two images on top of each other using the Overlay algorithm
  111. :rtype: :py:class:`~PIL.Image.Image`
  112. """
  113. image1.load()
  114. image2.load()
  115. return image1._new(image1.im.chop_overlay(image2.im))
  116. def add(image1, image2, scale=1.0, offset=0):
  117. """
  118. Adds two images, dividing the result by scale and adding the
  119. offset. If omitted, scale defaults to 1.0, and offset to 0.0.
  120. .. code-block:: python
  121. out = ((image1 + image2) / scale + offset)
  122. :rtype: :py:class:`~PIL.Image.Image`
  123. """
  124. image1.load()
  125. image2.load()
  126. return image1._new(image1.im.chop_add(image2.im, scale, offset))
  127. def subtract(image1, image2, scale=1.0, offset=0):
  128. """
  129. Subtracts two images, dividing the result by scale and adding the offset.
  130. If omitted, scale defaults to 1.0, and offset to 0.0.
  131. .. code-block:: python
  132. out = ((image1 - image2) / scale + offset)
  133. :rtype: :py:class:`~PIL.Image.Image`
  134. """
  135. image1.load()
  136. image2.load()
  137. return image1._new(image1.im.chop_subtract(image2.im, scale, offset))
  138. def add_modulo(image1, image2):
  139. """Add two images, without clipping the result.
  140. .. code-block:: python
  141. out = ((image1 + image2) % MAX)
  142. :rtype: :py:class:`~PIL.Image.Image`
  143. """
  144. image1.load()
  145. image2.load()
  146. return image1._new(image1.im.chop_add_modulo(image2.im))
  147. def subtract_modulo(image1, image2):
  148. """Subtract two images, without clipping the result.
  149. .. code-block:: python
  150. out = ((image1 - image2) % MAX)
  151. :rtype: :py:class:`~PIL.Image.Image`
  152. """
  153. image1.load()
  154. image2.load()
  155. return image1._new(image1.im.chop_subtract_modulo(image2.im))
  156. def logical_and(image1, image2):
  157. """Logical AND between two images.
  158. Both of the images must have mode "1". If you would like to perform a
  159. logical AND on an image with a mode other than "1", try
  160. :py:meth:`~PIL.ImageChops.multiply` instead, using a black-and-white mask
  161. as the second image.
  162. .. code-block:: python
  163. out = ((image1 and image2) % MAX)
  164. :rtype: :py:class:`~PIL.Image.Image`
  165. """
  166. image1.load()
  167. image2.load()
  168. return image1._new(image1.im.chop_and(image2.im))
  169. def logical_or(image1, image2):
  170. """Logical OR between two images.
  171. Both of the images must have mode "1".
  172. .. code-block:: python
  173. out = ((image1 or image2) % MAX)
  174. :rtype: :py:class:`~PIL.Image.Image`
  175. """
  176. image1.load()
  177. image2.load()
  178. return image1._new(image1.im.chop_or(image2.im))
  179. def logical_xor(image1, image2):
  180. """Logical XOR between two images.
  181. Both of the images must have mode "1".
  182. .. code-block:: python
  183. out = ((bool(image1) != bool(image2)) % MAX)
  184. :rtype: :py:class:`~PIL.Image.Image`
  185. """
  186. image1.load()
  187. image2.load()
  188. return image1._new(image1.im.chop_xor(image2.im))
  189. def blend(image1, image2, alpha):
  190. """Blend images using constant transparency weight. Alias for
  191. :py:func:`PIL.Image.blend`.
  192. :rtype: :py:class:`~PIL.Image.Image`
  193. """
  194. return Image.blend(image1, image2, alpha)
  195. def composite(image1, image2, mask):
  196. """Create composite using transparency mask. Alias for
  197. :py:func:`PIL.Image.composite`.
  198. :rtype: :py:class:`~PIL.Image.Image`
  199. """
  200. return Image.composite(image1, image2, mask)
  201. def offset(image, xoffset, yoffset=None):
  202. """Returns a copy of the image where data has been offset by the given
  203. distances. Data wraps around the edges. If ``yoffset`` is omitted, it
  204. is assumed to be equal to ``xoffset``.
  205. :param xoffset: The horizontal distance.
  206. :param yoffset: The vertical distance. If omitted, both
  207. distances are set to the same value.
  208. :rtype: :py:class:`~PIL.Image.Image`
  209. """
  210. if yoffset is None:
  211. yoffset = xoffset
  212. image.load()
  213. return image._new(image.im.offset(xoffset, yoffset))