Basic Image Operations

Python Pillow Color Conversions

Image Manipulation

Image Filtering

Image Enhancement and Correction

Image Analysis

Advanced Topics

  • Image Module
  • Python Pillow Useful Resources

    Selected Reading

    Python Pillow - ImageChops.composite() Function



    The ImageChops.composite() function is used to create a composite image by blending two images using a transparency mask. This function is an alias for the composite function in the main Image module.

    Syntax

    Following is the syntax of the method −

    PIL.ImageChops.composite(image1, image2, mask)
    

    Or

    PIL.Image.composite(image1, image2, mask)
    

    Parameters

    Here are the details of its parameters −

    • image1 − The first input image to composite.

    • image2 − The second input image to composite. It must have the same mode and size as the first image.

    • mask − A mask image. This image can have modes "1", "L", or "RGBA", and it must have the same size as the other two images.

    Return Value

    The return type of this function is an Image.

    Examples

    Example 1

    In this example, image1, image2, and mask are the input images, and the composite() function is used to blend them together based on the transparency information in the mask.

    from PIL import Image, ImageChops
    
    # Open the first image
    image1 = Image.open('Images/Light.jpg')
    
    # Open the second image with the same mode and size as the first image
    image2 = Image.open('Images/TP-W.jpg')
    
    # Open the mask image with mode "L" 
    mask = Image.open("Images/mask.jpg").convert("L")
    
    # Use the composite function from ImageChops to blend the images using the mask
    composite_image = ImageChops.composite(image1, image2, mask)
    
    # Display or save the resulting composite image
    image1.show()
    image2.show()
    mask.show()
    composite_image.show()
    

    Output

    Input Image 1

    light

    Input Image 2

    tp_w

    Input Mask

    mask

    Output Composite Image

    chops composite

    Example 2

    The provided example creates a solid gray image with a pixel value of 128 and uses it as a mask to blend two images together.

    from PIL import Image, ImageChops
    
    # Open the first image
    image1 = Image.open('Images/TP-W.jpg')
    
    # Open the second image with the same mode and size as the first image
    image2 = Image.open('Images/rose.jpg').resize(image1.size)
    
    # Create a solid gray mask image with a pixel value of 128
    mask = Image.new("L", image1.size, 128)
    
    # Use the composite function to blend the images using the mask
    composite_image = Image.composite(image1, image2, mask)
    
    # Display the input and resulting images
    image1.show()
    image2.show()
    mask.show()
    composite_image.show()
    

    Output

    Input Image 1

    tp_w

    Input Image 2

    red flower

    Input Mask

    mask image

    Output Composite Image

    imagechops composite

    Example 3

    This example creates a mask with a white rectangle on a black background and uses it to blend two images together.

    from PIL import Image, ImageDraw
    
    # Open the first image
    image1 = Image.open('Images/TP-W.jpg')
    
    # Open the second image with the same mode and size as the first image
    image2 = Image.open('Images/rose.jpg').resize(image1.size)
    
    # Create a mask image with a white rectangle on a black background
    mask = Image.new("L", image1.size, 0)
    draw = ImageDraw.Draw(mask)
    draw.rectangle((245, 45, 445, 345), fill=255)
    
    # Use the composite function to blend the images using the mask
    composite_image = Image.composite(image1, image2, mask)
    
    # Display the input and resulting images
    image1.show()
    image2.show()
    mask.show()
    composite_image.show()
    

    Output

    Input Image 1

    tp_w

    Input Image 2

    red flower

    Input Mask

    bw box

    Output Composite Image

    output composite image
    python_pillow_function_reference.htm
    Advertisements