ImageChops (“Channel Operations”) Module¶
Note
All source images in this document are derived from https://www.pexels.com (CC0 License).
Functions¶
add¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.add, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.add
from PIL import Image, ImageChops
img1 = Image.open("data/srcimg18.jpg")
img2 = Image.open("data/srcimg19.jpg")
dimg = ImageChops.add(img1, img2, scale=1.0)
dimg.save("result/ImageChops_add_01_s1.jpg")
dimg = ImageChops.add(img1, img2, scale=2.0)
dimg.save("result/ImageChops_add_01_s2.jpg")
|
scale=1.0 |
scale=2.0 |
add_modulo¶
from PIL import Image, ImageChops
img1 = Image.open("data/srcimg18.jpg")
img2 = Image.open("data/srcimg19.jpg")
dimg = ImageChops.add_modulo(img1, img2)
dimg.save("result/ImageChops_add_modulo_01.jpg")
|
blend¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.blend, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.blend
This function is an alias for PIL.Image.Image.blend().
from PIL import Image, ImageChops
# It seems two images must be same size for ImageChops.blend,
# but documents doesn't mention that.
img1 = Image.open("data/srcimg18.jpg")
img2 = Image.open("data/srcimg19.jpg").resize(img1.size)
dimg = ImageChops.blend(img1, img2, alpha=0.25)
dimg.save("result/ImageChops_blend_01_a025.jpg")
dimg = ImageChops.blend(img1, img2, alpha=0.5)
dimg.save("result/ImageChops_blend_01_a050.jpg")
dimg = ImageChops.blend(img1, img2, alpha=0.75)
dimg.save("result/ImageChops_blend_01_a075.jpg")
|
alpha=0.25 |
alpha=0.5 |
alpha=0.75 |
#
# NOTE: This demo works with pillow version >= 3.4.
#
from PIL import Image, ImageChops
# It seems two images must be same size for ImageChops.blend,
# but documents doesn't mention that.
img1 = Image.open("data/srcimg18.jpg")
img2 = Image.open("data/srcimg19.jpg").resize(img1.size)
# save as animation gif
# see:
# https://pillow.readthedocs.io/en/latest/handbook/image-file-formats.html#saving
# NOTE: file size of animation GIF grows very huge!
frames = [
ImageChops.blend(
img1, img2, alpha=1.0 / 32 * i).resize(
(img1.width // 4, img1.height // 4))
for i in range(32 + 1)
]
frames[0].save(
"result/ImageChops_blend_02.gif",
save_all=True,
append_images=frames[1:],
optimize=True,
duration=100, # [ms]
loop=255)
|
composite¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.composite, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.composite
This function is an alias for PIL.Image.Image.composite().
from PIL import Image, ImageChops
# It seems two images and mask must be same size for ImageChops.composite,
# but documents doesn't mention that.
img1 = Image.open("data/srcimg18.jpg")
img2 = Image.open("data/srcimg19.jpg").resize(img1.size)
#
mask = Image.open("data/mask_circle_01.jpg").resize(img1.size) # circle
#
dimg = ImageChops.composite(img1, img2, mask)
dimg.save("result/ImageChops_composite_01.jpg")
|
constant¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.constant, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.constant
PIL.ImageChops.constant(image, value)
is identical to PIL.Image.new("L", image.size, value)
.
darker¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.darker, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.darker
from PIL import Image, ImageChops
img1 = Image.open("data/srcimg18.jpg")
img2 = Image.open("data/srcimg19.jpg")
dimg = ImageChops.darker(img1, img2)
dimg.save("result/ImageChops_darker_01.jpg")
|
difference¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.difference, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.difference
from PIL import Image, ImageChops
img1 = Image.open("data/srcimg18.jpg")
img2 = Image.open("data/srcimg19.jpg")
dimg = ImageChops.difference(img1, img2)
dimg.save("result/ImageChops_difference_01.jpg")
|
duplicate¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.duplicate, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.duplicate
PIL.ImageChops.duplicate(image)
is identical to image.copy()
.
invert¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.invert, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.invert
from PIL import Image, ImageChops
img1 = Image.open("data/srcimg18.jpg")
dimg = ImageChops.invert(img1)
dimg.save("result/ImageChops_invert_01.jpg")
|
lighter¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.lighter, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.lighter
from PIL import Image, ImageChops
img1 = Image.open("data/srcimg18.jpg")
img2 = Image.open("data/srcimg19.jpg")
dimg = ImageChops.lighter(img1, img2)
dimg.save("result/ImageChops_lighter_01.jpg")
|
logical_and¶
from PIL import Image, ImageChops
# It seems modes of two images must be "1" for ImageChops.logical_and,
# but documents doesn't mention that...
img1 = Image.open("data/srcimg18.jpg").convert("1")
img2 = Image.open("data/srcimg19.jpg").convert("1")
dimg = ImageChops.logical_and(img1, img2)
dimg.save("result/ImageChops_logical_and_01.jpg")
|
logical_or¶
from PIL import Image, ImageChops
# It seems modes of two images must be "1" for ImageChops.logical_or,
# but documents doesn't mention that...
img1 = Image.open("data/srcimg18.jpg").convert("1")
img2 = Image.open("data/srcimg19.jpg").convert("1")
dimg = ImageChops.logical_or(img1, img2)
dimg.save("result/ImageChops_logical_or_01.jpg")
|
multiply¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.multiply, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.multiply
from PIL import Image, ImageChops
img1 = Image.open("data/srcimg18.jpg")
img2 = Image.open("data/srcimg19.jpg")
dimg = ImageChops.multiply(img1, img2)
dimg.save("result/ImageChops_multiply_01.jpg")
|
offset¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.offset, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.offset
from PIL import Image, ImageChops
img = Image.open("data/srcimg11.png")
dimg = ImageChops.offset(
img,
xoffset=img.size[0] // 3,
yoffset=img.size[1] // 3,
)
dimg.save("result/ImageChops_offset_01.jpg")
|
screen¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.screen, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.screen
from PIL import Image, ImageChops
img1 = Image.open("data/srcimg18.jpg")
img2 = Image.open("data/srcimg19.jpg")
dimg = ImageChops.screen(img1, img2)
dimg.save("result/ImageChops_screen_01.jpg")
|
subtract¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.subtract, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.subtract
from PIL import Image, ImageChops
img1 = Image.open("data/srcimg18.jpg")
img2 = Image.open("data/srcimg19.jpg")
dimg = ImageChops.subtract(img1, img2, scale=1.0)
dimg.save("result/ImageChops_subtract_01_s1.jpg")
dimg = ImageChops.subtract(img1, img2, scale=0.5)
dimg.save("result/ImageChops_subtract_01_s2.jpg")
|
scale=1.0 |
scale=0.5 |
subtract_modulo¶
- doc
https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.subtract_modulo, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.subtract_modulo
from PIL import Image, ImageChops
img1 = Image.open("data/srcimg18.jpg")
img2 = Image.open("data/srcimg19.jpg")
dimg = ImageChops.subtract_modulo(img1, img2)
dimg.save("result/ImageChops_subtract_modulo_01.jpg")
|