Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/PIL/ImageChops.py: 28%
77 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-07-17 14:22 -0600
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#
18from . import Image
21def constant(image, value):
22 """Fill a channel with a given grey level.
24 :rtype: :py:class:`~PIL.Image.Image`
25 """
27 return Image.new("L", image.size, value)
30def duplicate(image):
31 """Copy a channel. Alias for :py:meth:`PIL.Image.Image.copy`.
33 :rtype: :py:class:`~PIL.Image.Image`
34 """
36 return image.copy()
39def invert(image):
40 """
41 Invert an image (channel).
43 .. code-block:: python
45 out = MAX - image
47 :rtype: :py:class:`~PIL.Image.Image`
48 """
50 image.load()
51 return image._new(image.im.chop_invert())
54def lighter(image1, image2):
55 """
56 Compares the two images, pixel by pixel, and returns a new image containing
57 the lighter values.
59 .. code-block:: python
61 out = max(image1, image2)
63 :rtype: :py:class:`~PIL.Image.Image`
64 """
66 image1.load()
67 image2.load()
68 return image1._new(image1.im.chop_lighter(image2.im))
71def darker(image1, image2):
72 """
73 Compares the two images, pixel by pixel, and returns a new image containing
74 the darker values.
76 .. code-block:: python
78 out = min(image1, image2)
80 :rtype: :py:class:`~PIL.Image.Image`
81 """
83 image1.load()
84 image2.load()
85 return image1._new(image1.im.chop_darker(image2.im))
88def difference(image1, image2):
89 """
90 Returns the absolute value of the pixel-by-pixel difference between the two
91 images.
93 .. code-block:: python
95 out = abs(image1 - image2)
97 :rtype: :py:class:`~PIL.Image.Image`
98 """
100 image1.load()
101 image2.load()
102 return image1._new(image1.im.chop_difference(image2.im))
105def multiply(image1, image2):
106 """
107 Superimposes two images on top of each other.
109 If you multiply an image with a solid black image, the result is black. If
110 you multiply with a solid white image, the image is unaffected.
112 .. code-block:: python
114 out = image1 * image2 / MAX
116 :rtype: :py:class:`~PIL.Image.Image`
117 """
119 image1.load()
120 image2.load()
121 return image1._new(image1.im.chop_multiply(image2.im))
124def screen(image1, image2):
125 """
126 Superimposes two inverted images on top of each other.
128 .. code-block:: python
130 out = MAX - ((MAX - image1) * (MAX - image2) / MAX)
132 :rtype: :py:class:`~PIL.Image.Image`
133 """
135 image1.load()
136 image2.load()
137 return image1._new(image1.im.chop_screen(image2.im))
140def soft_light(image1, image2):
141 """
142 Superimposes two images on top of each other using the Soft Light algorithm
144 :rtype: :py:class:`~PIL.Image.Image`
145 """
147 image1.load()
148 image2.load()
149 return image1._new(image1.im.chop_soft_light(image2.im))
152def hard_light(image1, image2):
153 """
154 Superimposes two images on top of each other using the Hard Light algorithm
156 :rtype: :py:class:`~PIL.Image.Image`
157 """
159 image1.load()
160 image2.load()
161 return image1._new(image1.im.chop_hard_light(image2.im))
164def overlay(image1, image2):
165 """
166 Superimposes two images on top of each other using the Overlay algorithm
168 :rtype: :py:class:`~PIL.Image.Image`
169 """
171 image1.load()
172 image2.load()
173 return image1._new(image1.im.chop_overlay(image2.im))
176def add(image1, image2, scale=1.0, offset=0):
177 """
178 Adds two images, dividing the result by scale and adding the
179 offset. If omitted, scale defaults to 1.0, and offset to 0.0.
181 .. code-block:: python
183 out = ((image1 + image2) / scale + offset)
185 :rtype: :py:class:`~PIL.Image.Image`
186 """
188 image1.load()
189 image2.load()
190 return image1._new(image1.im.chop_add(image2.im, scale, offset))
193def subtract(image1, image2, scale=1.0, offset=0):
194 """
195 Subtracts two images, dividing the result by scale and adding the offset.
196 If omitted, scale defaults to 1.0, and offset to 0.0.
198 .. code-block:: python
200 out = ((image1 - image2) / scale + offset)
202 :rtype: :py:class:`~PIL.Image.Image`
203 """
205 image1.load()
206 image2.load()
207 return image1._new(image1.im.chop_subtract(image2.im, scale, offset))
210def add_modulo(image1, image2):
211 """Add two images, without clipping the result.
213 .. code-block:: python
215 out = ((image1 + image2) % MAX)
217 :rtype: :py:class:`~PIL.Image.Image`
218 """
220 image1.load()
221 image2.load()
222 return image1._new(image1.im.chop_add_modulo(image2.im))
225def subtract_modulo(image1, image2):
226 """Subtract two images, without clipping the result.
228 .. code-block:: python
230 out = ((image1 - image2) % MAX)
232 :rtype: :py:class:`~PIL.Image.Image`
233 """
235 image1.load()
236 image2.load()
237 return image1._new(image1.im.chop_subtract_modulo(image2.im))
240def logical_and(image1, image2):
241 """Logical AND between two images.
243 Both of the images must have mode "1". If you would like to perform a
244 logical AND on an image with a mode other than "1", try
245 :py:meth:`~PIL.ImageChops.multiply` instead, using a black-and-white mask
246 as the second image.
248 .. code-block:: python
250 out = ((image1 and image2) % MAX)
252 :rtype: :py:class:`~PIL.Image.Image`
253 """
255 image1.load()
256 image2.load()
257 return image1._new(image1.im.chop_and(image2.im))
260def logical_or(image1, image2):
261 """Logical OR between two images.
263 Both of the images must have mode "1".
265 .. code-block:: python
267 out = ((image1 or image2) % MAX)
269 :rtype: :py:class:`~PIL.Image.Image`
270 """
272 image1.load()
273 image2.load()
274 return image1._new(image1.im.chop_or(image2.im))
277def logical_xor(image1, image2):
278 """Logical XOR between two images.
280 Both of the images must have mode "1".
282 .. code-block:: python
284 out = ((bool(image1) != bool(image2)) % MAX)
286 :rtype: :py:class:`~PIL.Image.Image`
287 """
289 image1.load()
290 image2.load()
291 return image1._new(image1.im.chop_xor(image2.im))
294def blend(image1, image2, alpha):
295 """Blend images using constant transparency weight. Alias for
296 :py:func:`PIL.Image.blend`.
298 :rtype: :py:class:`~PIL.Image.Image`
299 """
301 return Image.blend(image1, image2, alpha)
304def composite(image1, image2, mask):
305 """Create composite using transparency mask. Alias for
306 :py:func:`PIL.Image.composite`.
308 :rtype: :py:class:`~PIL.Image.Image`
309 """
311 return Image.composite(image1, image2, mask)
314def offset(image, xoffset, yoffset=None):
315 """Returns a copy of the image where data has been offset by the given
316 distances. Data wraps around the edges. If ``yoffset`` is omitted, it
317 is assumed to be equal to ``xoffset``.
319 :param image: Input image.
320 :param xoffset: The horizontal distance.
321 :param yoffset: The vertical distance. If omitted, both
322 distances are set to the same value.
323 :rtype: :py:class:`~PIL.Image.Image`
324 """
326 if yoffset is None:
327 yoffset = xoffset
328 image.load()
329 return image._new(image.im.offset(xoffset, yoffset))