ImageChops.py 6.9 KB

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