Search code examples
image-processingpython-imaging-library

PIllow Image.paste v.s composite v.s. alpha_composite v.s. blend, what's the difference?


Newbie in image processing. I'm confused with these methods when merging two images with Pillow:

PIL.Image.Image

  • .paste()
  • .composite()
  • .alpha_composite()
  • .blend()

Could anyone provide a quick explanation? Or where could I grab the related background knowledge?


Solution

  • I see it like this:

    • blend is the simplest. It takes a fixed and constant proportion of each image at each pixel location, e.g. 30% of image A and 70% of image B at each location all over the image. The ratio is a single number. This operation is not really interested in transparency, it is more of a weighted average where a part of both input images will be visible at every pixel location in the output image

    • paste and composite are synonyms. They use a mask, with the same size as the images, and take a proportion of image A and image B according to the value of the mask which may be different at each location. So you might have a 0-100 proportion of image A and image B at the top and 100-0 proportion at the bottom, and this would look like a smoothly blended transition from one image at the top to the other image at the bottom. Or, it may be like a largely opaque foreground where you only see one input image, but a transparent window through which you see the other input image. The mask, of the same size as the two input images, is key here and it can assume different values at different locations.

    • alpha compositing is the most complicated and is best described by Wikipedia


    Put another way, blend is no alpha/transparency channel and a fixed proportion of each input image present throughout the output image.

    paste is a single alpha channel that can vary across the image.

    alpha_composite is two alpha channels that can both vary across the image.