简体   繁体   中英

How can I get my loop to iterate through my list?

I'm new to programming and python. I've built a loop that creates a tiled image using one image, and it works great.

for left in range(0,iWidth,(logoWidth + xOffset)):
    for top in range (0,iHeight,(logoHeight + yOffset)):
        icopyIm2.paste(logo,(left,top))

icopyIm2.save("tiled_image.png")

However, I'd like it to use a series of images, such that each tile is different. In the parameters for "paste", the "logo" variable is only one image. I'd like the loop to iterate through a list, for example:

imageList = [pic1.png, pic2.png, pic3.png, pic4.png]

I'm not sure how to achieve this.

As stated in comments, you need to figure out how you want to handle the different images when you tile them. What do you want it to look like if your frame is 3x3 and you have 5 images?

I think the heart of your question is setting up a function with parameters. The below example makes a tiled images of 3-letter words, which I think is inline with your question. I have chosen to just loop through the list of inputs repeatedly--no pattern.

def make_box(width, height, hits):
    hit_limit = len(hits)
    count = 0
    for i in range(height):
        for j in range(width):
            print(hits[count % hit_limit], end=' ')
            count += 1
        print()

hit_list = ['bop', 'pow', 'zap', 'bam', 'oof']
box_h = 3
box_w = 4

make_box(box_w, box_h, hit_list)

Output:

bop pow zap bam 
oof bop pow zap 
bam oof bop pow 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM