En este artículo, aprenderemos cómo copiar una imagen sobre otra imagen usando la biblioteca de Pillows. Usaremos el módulo de imagen de la Pillow y los métodos copiar() y pegar() para lograr esta tarea.
Tendremos que crear copias de ambas imágenes para que no afecte a la imagen original con la ayuda del método copy() y luego pegar la imagen en la otra imagen con la ayuda del método paste().
Imágenes de entrada –
Imagen1:
Imagen2:
Ejemplo 1:
Python3
# import image module from pillow from PIL import Image # open the image Image1 = Image.open('D:\cat.jpg') # make a copy the image so that the # original image does not get affected Image1copy = Image1.copy() Image2 = Image.open('D:\core.jpg') Image2copy = Image2.copy() # paste image giving dimensions Image1copy.paste(Image2copy, (0, 0)) # save the image Image1copy.save('D:\pasted2.png')
Producción:
Ejemplo 2: Cambio de parámetros para colocar la Imagen2 en la cara del gato en la Imagen1.
Python3
# import image module from pillow from PIL import Image # open the image Image1 = Image.open('D:\cat.jpg') # make a copy the image so that # the original image does not get affected Image1copy = Image1.copy() Image2 = Image.open('D:\core.jpg') Image2copy = Image2.copy() # paste image giving dimensions Image1copy.paste(Image2copy, (70, 150)) # save the image Image1copy.save('D:\pasted2.png')
Producción: