9951 explained code solutions for 126 technologies

python-pillowUsing mask to merge two images with transparency

In order to paste image and maintain its transparency, we have to pass the 3rd argument of paste() method and make it the same as 1st:

from PIL import Image

im = Image.open('/var/www/examples/heroine.png')
im_merge = Image.open('/var/www/examples/hammer.png')

im.paste(im_merge, (40, 500), im_merge)

im.show()ctrl + c

import Pillow package modules


open given image with Pillow


paste given image to the current image


image to paste to im

(40, 500)

x and y coordinates to paste image to


displays resulting image