Image Composite Function

  • Share this:

Code introduction


This function composes two images by placing the second image on top of the first one at a specified position.


Technology Stack : Pillow

Code Type : Function

Code Difficulty : Intermediate


                
                    
def image_composite(image1, image2, position=(0, 0)):
    """
    Compose two images by placing the second image on top of the first one.
    """
    # Ensure that both images have the same size
    if image1.size != image2.size:
        raise ValueError("Both images must have the same size.")

    # Create a new image with the size of the first image
    output_image = Image.new('RGB', image1.size)

    # Paste the first image onto the new image
    output_image.paste(image1, (0, 0))

    # Paste the second image onto the new image at the specified position
    output_image.paste(image2, position)

    return output_image                
              
Tags: