Python Forum

Full Version: Pasting multiple images on to one image with a for loop.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Using PIL I want to paste multiple images (currently the same, later all different) on to a single image.

I have tried this:
import PIL

img = Image.new('RGB', (1000, 1000), color = (0,0,0))
img.save('out.png')

for i in range(0, 4):
    out = Image.open('out.png')
    out.paste(Image.open('the image i want to use'), (0, i))
    out.save('out.png')              
    
but I only see one image, not multiple, how do I fix this?

Thanks,
Dream
Hello, if you want to put them 4 times next to each other then:
from PIL import Image

img_to_paste = Image.open('/path/to/image')
width, height = img_to_paste.size

n = 4   # paste image n-times

img = Image.new('RGB', (width*n, height), color=(0, 0, 0))     # set width, height of new image based on original image
img.save('out.png')

for i in range(0, n):
    out = Image.open('out.png')
    out.paste(img_to_paste, (i*width, 0))    # the second argument here is tuple representing upper left corner
    out.save('out.png')
The biggest problem in your code was that your where pasting your picture on positions (0, 0) , (0, 1) etc so they were basically pasting on top of each other.
That has done it! Thanks!

Dream