Paste another image into an image with Python, Pillow
paste() for pasting an image is provided in the
Image module of Pillow (PIL).
Please refer to the following post 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
PIL and open the base (background) image and the image to paste.
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 = Image.open('data/src/rocket.jpg') im2 = Image.open('data/src/lena.jpg')
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) im1.save('data/dst/rocket_pillow_paste.jpg', quality=95)
paste() overwrites the base image itself, so if you want to keep the original image, use the copied image with
im1 = Image.open('data/src/rocket.jpg') im2 = Image.open('data/src/lena.jpg') back_im = im1.copy() back_im.paste(im2) back_im.save('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
back_im = im1.copy() back_im.paste(im2, (100, 50)) back_im.save('data/dst/rocket_pillow_paste_pos.jpg', quality=95)
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)) back_im.save('data/dst/rocket_pillow_paste_out.jpg', quality=95)
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
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 = Image.new("L", im2.size, 0) draw = ImageDraw.Draw(mask_im) draw.ellipse((140, 50, 260, 170), fill=255) mask_im.save('data/dst/mask_circle.jpg', quality=95)
See the following post 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) back_im.save('data/dst/rocket_pillow_paste_mask_circle.jpg', quality=95)
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)) mask_im_blur.save('data/dst/mask_circle_blur.jpg', quality=95) back_im = im1.copy() back_im.paste(im2, (0, 0), mask_im_blur) back_im.save('data/dst/rocket_pillow_paste_mask_circle_blur.jpg', quality=95)
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: skimage.data.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
mask_im = Image.open('data/src/horse.png').resize(im2.size).convert('L') back_im = im1.copy() back_im.paste(im2, (100, 50), mask_im) back_im.save('data/dst/rocket_pillow_paste_mask_horse.jpg', quality=95)
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 post.