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

srcimg18.jpg srcimg18

srcimg19.jpg srcimg19

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 ImageChops.add.res01

scale=2.0 ImageChops.add.res02

add_modulo

doc

https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.add_modulo

srcimg18.jpg srcimg18

srcimg19.jpg srcimg19

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")

ImageChops.add_modulo.res01

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().

srcimg18.jpg srcimg18

srcimg19.jpg srcimg19

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 ImageChops.blend.res01

alpha=0.5 ImageChops.blend.res02

alpha=0.75 ImageChops.blend.res03

#
# 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)

ImageChops.blend.res04

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().

srcimg18.jpg srcimg18

srcimg19.jpg srcimg19

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")

ImageChops.composite.res01

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

srcimg18.jpg srcimg18

srcimg19.jpg srcimg19

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")

ImageChops.darker.res01

difference

doc

https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.difference, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.difference

srcimg18.jpg srcimg18

srcimg19.jpg srcimg19

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")

ImageChops.difference.res01

invert

doc

https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.invert, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.invert

srcimg18.jpg srcimg18

from PIL import Image, ImageChops

img1 = Image.open("data/srcimg18.jpg")

dimg = ImageChops.invert(img1)
dimg.save("result/ImageChops_invert_01.jpg")

ImageChops.invert.res01

lighter

doc

https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.lighter, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.lighter

srcimg18.jpg srcimg18

srcimg19.jpg srcimg19

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")

ImageChops.lighter.res01

logical_and

doc

https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.logical_and

srcimg18.jpg srcimg18

srcimg19.jpg srcimg19

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")

ImageChops.logical_and.res01

logical_or

doc

https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.logical_or

srcimg18.jpg srcimg18

srcimg19.jpg srcimg19

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")

ImageChops.logical_or.res01

multiply

doc

https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.multiply, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.multiply

srcimg18.jpg srcimg18

srcimg19.jpg srcimg19

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")

ImageChops.multiply.res01

offset

doc

https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.offset, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.offset

srcimg11.png srcimg11

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")

ImageChops.offset.res01

screen

doc

https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.screen, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.screen

srcimg18.jpg srcimg18

srcimg19.jpg srcimg19

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")

ImageChops.screen.res01

subtract

doc

https://pillow.readthedocs.io/en/latest/reference/ImageChops.html#PIL.ImageChops.subtract, http://effbot.org/imagingbook/imagechops.htm#tag-ImageChops.subtract

srcimg18.jpg srcimg18

srcimg19.jpg srcimg19

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 ImageChops.subtract.res01

scale=0.5 ImageChops.subtract.res02

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

srcimg18.jpg srcimg18

srcimg19.jpg srcimg19

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")

ImageChops.subtract_modulo.res01