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
PIL

import Pillow package modules

Image.open

open given image with Pillow

.paste(

paste given image to the current image

im_merge

image to paste to im

(40, 500)

x and y coordinates to paste image to

.show()

displays resulting image


Using mask to merge two images with transparency, python pillow

Usage example

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