Skip to content

Instantly share code, notes, and snippets.

@asears
Created November 30, 2021 12:17
Show Gist options
  • Select an option

  • Save asears/409f52a0164719129784ee0efbdac861 to your computer and use it in GitHub Desktop.

Select an option

Save asears/409f52a0164719129784ee0efbdac861 to your computer and use it in GitHub Desktop.
Overlay images with PIL
# https://moonbooks.org/Articles/How-to-overlay--superimpose-two-images-using-python-and-pillow-/
from PIL import Image
import numpy as np
img = Image.open("data_mask_1354_2030.png")
background = Image.open("background_1354_2030.png")
background.paste(img, (0, 0), img)
background.save('how_to_superimpose_two_images_01.png',"PNG")
# transparency
new_image = Image.new("RGBA", img.size, "WHITE")
from PIL import Image
import numpy as np
img = Image.open("data_mask_1354_2030.png")
print(img.size)
background = Image.open("background_730_1097.png")
print(background.size)
# resize the image
size = (1354,2030)
background = background.resize(size,Image.ANTIALIAS)
background.paste(img, (0, 0), img)
background.save('how_to_superimpose_two_images_02.png',"PNG")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment