ImageChops.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. At least one of the images must have mode "1".
  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. At least one of the images must have mode "1".
  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. At least one of the images must have mode "1".
  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. At least
  75. one of the images must have mode "1".
  76. .. code-block:: python
  77. out = image1 * image2 / MAX
  78. :rtype: :py:class:`~PIL.Image.Image`
  79. """
  80. image1.load()
  81. image2.load()
  82. return image1._new(image1.im.chop_multiply(image2.im))
  83. def screen(image1, image2):
  84. """
  85. Superimposes two inverted images on top of each other. At least one of the
  86. images must have mode "1".
  87. .. code-block:: python
  88. out = MAX - ((MAX - image1) * (MAX - image2) / MAX)
  89. :rtype: :py:class:`~PIL.Image.Image`
  90. """
  91. image1.load()
  92. image2.load()
  93. return image1._new(image1.im.chop_screen(image2.im))
  94. def add(image1, image2, scale=1.0, offset=0):
  95. """
  96. Adds two images, dividing the result by scale and adding the
  97. offset. If omitted, scale defaults to 1.0, and offset to 0.0.
  98. At least one of the images must have mode "1".
  99. .. code-block:: python
  100. out = ((image1 + image2) / scale + offset)
  101. :rtype: :py:class:`~PIL.Image.Image`
  102. """
  103. image1.load()
  104. image2.load()
  105. return image1._new(image1.im.chop_add(image2.im, scale, offset))
  106. def subtract(image1, image2, scale=1.0, offset=0):
  107. """
  108. Subtracts two images, dividing the result by scale and adding the offset.
  109. If omitted, scale defaults to 1.0, and offset to 0.0. At least one of the
  110. images must have mode "1".
  111. .. code-block:: python
  112. out = ((image1 - image2) / scale + offset)
  113. :rtype: :py:class:`~PIL.Image.Image`
  114. """
  115. image1.load()
  116. image2.load()
  117. return image1._new(image1.im.chop_subtract(image2.im, scale, offset))
  118. def add_modulo(image1, image2):
  119. """Add two images, without clipping the result. At least one of the images
  120. must have mode "1".
  121. .. code-block:: python
  122. out = ((image1 + image2) % MAX)
  123. :rtype: :py:class:`~PIL.Image.Image`
  124. """
  125. image1.load()
  126. image2.load()
  127. return image1._new(image1.im.chop_add_modulo(image2.im))
  128. def subtract_modulo(image1, image2):
  129. """Subtract two images, without clipping the result. At least one of the
  130. images must have mode "1".
  131. .. code-block:: python
  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_subtract_modulo(image2.im))
  138. def logical_and(image1, image2):
  139. """Logical AND between two images. At least one of the images must have
  140. mode "1".
  141. .. code-block:: python
  142. out = ((image1 and image2) % MAX)
  143. :rtype: :py:class:`~PIL.Image.Image`
  144. """
  145. image1.load()
  146. image2.load()
  147. return image1._new(image1.im.chop_and(image2.im))
  148. def logical_or(image1, image2):
  149. """Logical OR between two images. At least one of the images must have
  150. mode "1".
  151. .. code-block:: python
  152. out = ((image1 or image2) % MAX)
  153. :rtype: :py:class:`~PIL.Image.Image`
  154. """
  155. image1.load()
  156. image2.load()
  157. return image1._new(image1.im.chop_or(image2.im))
  158. def logical_xor(image1, image2):
  159. """Logical XOR between two images. At least one of the images must have
  160. mode "1".
  161. .. code-block:: python
  162. out = ((bool(image1) != bool(image2)) % MAX)
  163. :rtype: :py:class:`~PIL.Image.Image`
  164. """
  165. image1.load()
  166. image2.load()
  167. return image1._new(image1.im.chop_xor(image2.im))
  168. def blend(image1, image2, alpha):
  169. """Blend images using constant transparency weight. Alias for
  170. :py:meth:`PIL.Image.Image.blend`.
  171. :rtype: :py:class:`~PIL.Image.Image`
  172. """
  173. return Image.blend(image1, image2, alpha)
  174. def composite(image1, image2, mask):
  175. """Create composite using transparency mask. Alias for
  176. :py:meth:`PIL.Image.Image.composite`.
  177. :rtype: :py:class:`~PIL.Image.Image`
  178. """
  179. return Image.composite(image1, image2, mask)
  180. def offset(image, xoffset, yoffset=None):
  181. """Returns a copy of the image where data has been offset by the given
  182. distances. Data wraps around the edges. If **yoffset** is omitted, it
  183. is assumed to be equal to **xoffset**.
  184. :param xoffset: The horizontal distance.
  185. :param yoffset: The vertical distance. If omitted, both
  186. distances are set to the same value.
  187. :rtype: :py:class:`~PIL.Image.Image`
  188. """
  189. if yoffset is None:
  190. yoffset = xoffset
  191. image.load()
  192. return image._new(image.im.offset(xoffset, yoffset))