I'm trying to write a NFT generator that has multiple folders such as head, eyes, background
that all contain images. My code takes a random image from each folder and puts the paths of these images in a list.
So I have a list called image_items
that contains the path of all images to create a unique new image.
Every image has a transparent background and has to be put on top of the previous image. So I'll have a new image created and I need to paste the first image in the list on top of it, then paste the second image in the list on top of that and so on.
I've tried doing this with the following code:
from PIL import Image
import PIL
image = Image.open(image_items[0]) # Takes size of first image in list (all sizes are the same)
height, width = image.size
new_image = PIL.Image.new(mode='RGBA', size=(height, width))
for i in image_items:
new_image.paste(Image.open(i), (0,0))
new_image.save('test.png')
The only problem is that it isn't pasting the images on top of eachother. It looks like it's just saving the last image in the list as test.png
. Does anyone know what I'm doing wrong?
Basically How to overlay images in Python is what I need, but instead of putting two images on top of each other, I need to put all images in the image_items
list on top of each other: