2
# The Python Imaging Library.
5
# standard channel operations
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)
12
# Copyright (c) 1997-2000 by Secret Labs AB
13
# Copyright (c) 1996-2000 by Fredrik Lundh
15
# See the README file for information on usage and redistribution.
21
# The <b>ImageChops</b> module contains a number of arithmetical image
22
# operations, called <i>channel operations</i> ("chops"). These can be
23
# used for various purposes, including special effects, image
24
# compositions, algorithmic painting, and more.
26
# At this time, channel operations are only implemented for 8-bit
27
# images (e.g. "L" and "RGB").
29
# Most channel operations take one or two image arguments and returns
30
# a new image. Unless otherwise noted, the result of a channel
31
# operation is always clipped to the range 0 to MAX (which is 255 for
32
# all modes supported by the operations in this module).
36
# Return an image with the same size as the given image, but filled
37
# with the given pixel value.
39
# @param image Reference image.
40
# @param value Pixel value.
41
# @return An image object.
43
def constant(image, value):
44
"Fill a channel with a given grey level"
46
return Image.new("L", image.size, value)
51
# @param image Source image.
52
# @return A copy of the source image.
55
"Create a copy of a channel"
63
# @param image Source image.
64
# @return An image object.
70
return image._new(image.im.chop_invert())
73
# Compare images, and return lighter pixel value
74
# (max(image1, image2)).
76
# Compares the two images, pixel by pixel, and returns a new image
77
# containing the lighter values.
79
# @param image1 First image.
80
# @param image1 Second image.
81
# @return An image object.
83
def lighter(image1, image2):
84
"Select the lighter pixels from each image"
88
return image1._new(image1.im.chop_lighter(image2.im))
91
# Compare images, and return darker pixel value
92
# (min(image1, image2)).
94
# Compares the two images, pixel by pixel, and returns a new image
95
# containing the darker values.
97
# @param image1 First image.
98
# @param image1 Second image.
99
# @return An image object.
101
def darker(image1, image2):
102
"Select the darker pixels from each image"
106
return image1._new(image1.im.chop_darker(image2.im))
109
# Calculate absolute difference
110
# (abs(image1 - image2)).
112
# Returns the absolute value of the difference between the two images.
114
# @param image1 First image.
115
# @param image1 Second image.
116
# @return An image object.
118
def difference(image1, image2):
119
"Subtract one image from another"
123
return image1._new(image1.im.chop_difference(image2.im))
126
# Superimpose positive images
127
# (image1 * image2 / MAX).
129
# Superimposes two images on top of each other. If you multiply an
130
# image with a solid black image, the result is black. If you multiply
131
# with a solid white image, the image is unaffected.
133
# @param image1 First image.
134
# @param image1 Second image.
135
# @return An image object.
137
def multiply(image1, image2):
138
"Superimpose two positive images"
142
return image1._new(image1.im.chop_multiply(image2.im))
145
# Superimpose negative images
146
# (MAX - ((MAX - image1) * (MAX - image2) / MAX)).
148
# Superimposes two inverted images on top of each other.
150
# @param image1 First image.
151
# @param image1 Second image.
152
# @return An image object.
154
def screen(image1, image2):
155
"Superimpose two negative images"
159
return image1._new(image1.im.chop_screen(image2.im))
163
# ((image1 + image2) / scale + offset).
165
# Adds two images, dividing the result by scale and adding the
166
# offset. If omitted, scale defaults to 1.0, and offset to 0.0.
168
# @param image1 First image.
169
# @param image1 Second image.
170
# @return An image object.
172
def add(image1, image2, scale=1.0, offset=0):
177
return image1._new(image1.im.chop_add(image2.im, scale, offset))
181
# ((image1 - image2) / scale + offset).
183
# Subtracts two images, dividing the result by scale and adding the
184
# offset. If omitted, scale defaults to 1.0, and offset to 0.0.
186
# @param image1 First image.
187
# @param image1 Second image.
188
# @return An image object.
190
def subtract(image1, image2, scale=1.0, offset=0):
191
"Subtract two images"
195
return image1._new(image1.im.chop_subtract(image2.im, scale, offset))
198
# Add images without clipping
199
# ((image1 + image2) % MAX).
201
# Adds two images, without clipping the result.
203
# @param image1 First image.
204
# @param image1 Second image.
205
# @return An image object.
207
def add_modulo(image1, image2):
208
"Add two images without clipping"
212
return image1._new(image1.im.chop_add_modulo(image2.im))
215
# Subtract images without clipping
216
# ((image1 - image2) % MAX).
218
# Subtracts two images, without clipping the result.
220
# @param image1 First image.
221
# @param image1 Second image.
222
# @return An image object.
224
def subtract_modulo(image1, image2):
225
"Subtract two images without clipping"
229
return image1._new(image1.im.chop_subtract_modulo(image2.im))
233
# (image1 and image2).
235
def logical_and(image1, image2):
236
"Logical and between two images"
240
return image1._new(image1.im.chop_and(image2.im))
244
# (image1 or image2).
246
def logical_or(image1, image2):
247
"Logical or between two images"
251
return image1._new(image1.im.chop_or(image2.im))
255
# (image1 xor image2).
257
def logical_xor(image1, image2):
258
"Logical xor between two images"
262
return image1._new(image1.im.chop_xor(image2.im))
265
# Blend images using constant transparency weight.
267
# Same as the <b>blend</b> function in the <b>Image</b> module.
269
def blend(image1, image2, alpha):
270
"Blend two images using a constant transparency weight"
272
return Image.blend(image1, image2, alpha)
275
# Create composite using transparency mask.
277
# Same as the <b>composite</b> function in the <b>Image</b> module.
279
def composite(image1, image2, mask):
280
"Create composite image by blending images using a transparency mask"
282
return Image.composite(image1, image2, mask)
287
# Returns a copy of the image where data has been offset by the given
288
# distances. Data wraps around the edges. If yoffset is omitted, it
289
# is assumed to be equal to xoffset.
291
# @param image Source image.
292
# @param xoffset The horizontal distance.
293
# @param yoffset The vertical distance. If omitted, both
294
# distances are set to the same value.
295
# @return An Image object.
297
def offset(image, xoffset, yoffset=None):
298
"Offset image in horizontal and/or vertical direction"
302
return image._new(image.im.offset(xoffset, yoffset))