You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
6.0 KiB

4 years ago
  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
  51. containing 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 add(image1, image2, scale=1.0, offset=0):
  93. """
  94. Adds two images, dividing the result by scale and adding the
  95. offset. If omitted, scale defaults to 1.0, and offset to 0.0.
  96. .. code-block:: python
  97. out = ((image1 + image2) / scale + offset)
  98. :rtype: :py:class:`~PIL.Image.Image`
  99. """
  100. image1.load()
  101. image2.load()
  102. return image1._new(image1.im.chop_add(image2.im, scale, offset))
  103. def subtract(image1, image2, scale=1.0, offset=0):
  104. """
  105. Subtracts two images, dividing the result by scale and adding the
  106. offset. If omitted, scale defaults to 1.0, and offset to 0.0.
  107. .. code-block:: python
  108. out = ((image1 - image2) / scale + offset)
  109. :rtype: :py:class:`~PIL.Image.Image`
  110. """
  111. image1.load()
  112. image2.load()
  113. return image1._new(image1.im.chop_subtract(image2.im, scale, offset))
  114. def add_modulo(image1, image2):
  115. """Add two images, without clipping the result.
  116. .. code-block:: python
  117. out = ((image1 + image2) % MAX)
  118. :rtype: :py:class:`~PIL.Image.Image`
  119. """
  120. image1.load()
  121. image2.load()
  122. return image1._new(image1.im.chop_add_modulo(image2.im))
  123. def subtract_modulo(image1, image2):
  124. """Subtract two images, without clipping the result.
  125. .. code-block:: python
  126. out = ((image1 - image2) % MAX)
  127. :rtype: :py:class:`~PIL.Image.Image`
  128. """
  129. image1.load()
  130. image2.load()
  131. return image1._new(image1.im.chop_subtract_modulo(image2.im))
  132. def logical_and(image1, image2):
  133. """Logical AND between two images.
  134. .. code-block:: python
  135. out = ((image1 and image2) % MAX)
  136. :rtype: :py:class:`~PIL.Image.Image`
  137. """
  138. image1.load()
  139. image2.load()
  140. return image1._new(image1.im.chop_and(image2.im))
  141. def logical_or(image1, image2):
  142. """Logical OR between two images.
  143. .. code-block:: python
  144. out = ((image1 or image2) % MAX)
  145. :rtype: :py:class:`~PIL.Image.Image`
  146. """
  147. image1.load()
  148. image2.load()
  149. return image1._new(image1.im.chop_or(image2.im))
  150. def logical_xor(image1, image2):
  151. """Logical XOR between two images.
  152. .. code-block:: python
  153. out = ((bool(image1) != bool(image2)) % MAX)
  154. :rtype: :py:class:`~PIL.Image.Image`
  155. """
  156. image1.load()
  157. image2.load()
  158. return image1._new(image1.im.chop_xor(image2.im))
  159. def blend(image1, image2, alpha):
  160. """Blend images using constant transparency weight. Alias for
  161. :py:meth:`PIL.Image.Image.blend`.
  162. :rtype: :py:class:`~PIL.Image.Image`
  163. """
  164. return Image.blend(image1, image2, alpha)
  165. def composite(image1, image2, mask):
  166. """Create composite using transparency mask. Alias for
  167. :py:meth:`PIL.Image.Image.composite`.
  168. :rtype: :py:class:`~PIL.Image.Image`
  169. """
  170. return Image.composite(image1, image2, mask)
  171. def offset(image, xoffset, yoffset=None):
  172. """Returns a copy of the image where data has been offset by the given
  173. distances. Data wraps around the edges. If **yoffset** is omitted, it
  174. is assumed to be equal to **xoffset**.
  175. :param xoffset: The horizontal distance.
  176. :param yoffset: The vertical distance. If omitted, both
  177. distances are set to the same value.
  178. :rtype: :py:class:`~PIL.Image.Image`
  179. """
  180. if yoffset is None:
  181. yoffset = xoffset
  182. image.load()
  183. return image._new(image.im.offset(xoffset, yoffset))