Paste another image into an image with Python, Pillow

Posted: 2019-05-14 / Tags: Python, Pillow, Image Processing

A method paste() for pasting an image is provided in the Image module of Pillow (PIL).

Please refer to the following article for the installation and basic usage of Pillow (PIL).

Here, the following three will be described.

  • Normal paste
  • Specify the position to paste
  • Use mask image

Import Image from PIL and open the base (background) image and the image to paste.

ImageDraw and ImageFilter are used to set the mask image described last. You may omit it if you simply paste it.

from PIL import Image, ImageDraw, ImageFilter

im1 ='data/src/rocket.jpg')
im2 ='data/src/lena.jpg')

base image image to paste

Normal paste

Call the paste() method from the background image and set the image to paste.

By default, the image is pasted at the position where the upper left of the paste image is the origin (upper left) of the base image.

im1.paste(im2)'data/dst/rocket_pillow_paste.jpg', quality=95)

Pillow Image.paste result

paste() overwrites the base image itself, so if you want to keep the original image, use the copied image with copy().

im1 ='data/src/rocket.jpg')
im2 ='data/src/lena.jpg')

back_im = im1.copy()
back_im.paste(im2)'data/dst/rocket_pillow_paste.jpg', quality=95)

Specify the position to paste

The position to paste is specified by a tuple (x coordinate in upper left, y coordinate in upper left) in the second parameter box.

back_im = im1.copy()
back_im.paste(im2, (100, 50))'data/dst/rocket_pillow_paste_pos.jpg', quality=95)

Pillow Image.paste result set position

If the pasted image extends outside the region of the base image, the area that extends is ignored.

back_im = im1.copy()
back_im.paste(im2, (400, 100))'data/dst/rocket_pillow_paste_out.jpg', quality=95)

Pillow Image.paste result set position extend

Use mask image

If a mask image is specified as the third parameter mask, the pasted image can be cut out and pasted not only in a rectangle but in various shapes.

The image that can be used as a mask image has the same size as the pasted image and the following three types of mode.

  • 1: 1 bit image (binary image)
  • L: 8-bit grayscale image
  • RGBA: Image with alpha channel

When the mask image is 8-bit gray scale (mode='L'), the base image is output when the value of the mask image is 0 (black), and the pasted image is output when 255 (white). For other values, the two images are blended according to the value.

Draw a white circle on a black background with the ImageDraw module to generate a mask image.

mask_im ="L", im2.size, 0)
draw = ImageDraw.Draw(mask_im)
draw.ellipse((140, 50, 260, 170), fill=255)'data/dst/mask_circle.jpg', quality=95)

Pillow Image.paste mask circle

See the following article for drawing with Pillow.

Paste processing is performed using this image as a mask. You can mask the pasted image into a circle.

back_im = im1.copy()
back_im.paste(im2, (0, 0), mask_im)'data/dst/rocket_pillow_paste_mask_circle.jpg', quality=95)

Pillow Image.paste result circular mask

When the mask image is blurred using the ImageFilter module, the boundaries can be blended and pasted.

mask_im_blur = mask_im.filter(ImageFilter.GaussianBlur(10))'data/dst/mask_circle_blur.jpg', quality=95)

back_im = im1.copy()
back_im.paste(im2, (0, 0), mask_im_blur)'data/dst/rocket_pillow_paste_mask_circle_blur.jpg', quality=95)

Pillow Image.paste mask blurred circle Pillow Image.paste result blurred circle mask

Not limited to geometric figures drawn by ImageDraw, they can be pasted in any shape as long as a mask image is prepared.

Try using a black and white horse-shaped image (scikit-image sample:

Pillow Image.paste mask horse

After the image is read by open(), it is adjusted to the size of the pasted image by resize(), and the mode is converted to 'L' (gray scale) by convert().

mask_im ='data/src/horse.png').resize(im2.size).convert('L')

back_im = im1.copy()
back_im.paste(im2, (100, 50), mask_im)'data/dst/rocket_pillow_paste_mask_horse.jpg', quality=95)

Pillow Image.paste result horse mask

Although not a good example, it is pasted in a complicated shape. If you prepare images of star or heart shape, it can be used in various situations.

If you want to reverse the black and white of the mask image, please refer to the following article.

Sponsored Link

Related Categories

Related Articles