Python Forum
Pasting multiple images on to one image with a for loop.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pasting multiple images on to one image with a for loop.
#1
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
Reply
#2
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.
Reply
#3
That has done it! Thanks!

Dream
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to loop through code to plot seaborn line plots across multiple subplots eyavuz21 0 1,651 Dec-05-2022, 10:46 AM
Last Post: eyavuz21
  Multiple Loop Statements in a Variable Dexty 1 1,198 May-23-2022, 08:53 AM
Last Post: bowlofred
  Why is copying and pasting a block now broken? WagmoreBarkless 2 1,384 May-05-2022, 05:01 AM
Last Post: WagmoreBarkless
  Why is copying and pasting a block now broken? WagmoreBarkless 1 1,225 May-04-2022, 11:40 PM
Last Post: Larz60+
  Multiple user defined plots with secondary axes using for loop maltp 1 1,436 Apr-30-2022, 10:19 AM
Last Post: maltp
Question How can I merge several images via loop using the information of a dataframe? noahverner1995 1 1,423 Dec-31-2021, 05:03 AM
Last Post: noahverner1995
  Trying to separate a loop across multiple threads stylingpat 0 1,670 May-05-2021, 05:21 PM
Last Post: stylingpat
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,921 Dec-02-2020, 07:50 AM
Last Post: Gilush
  Multiple MEAN value using FOR loop kka_anand 2 2,154 Jul-14-2020, 06:20 PM
Last Post: kka_anand
  copy/pasting in excel WHILE keep file format zarize 0 1,943 Jun-23-2020, 03:51 PM
Last Post: zarize

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020