[英]How can I get my loop to iterate through my list?
I'm new to programming and python. 我是编程和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] 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?
如果您的帧是3x3并且有5张图像,您希望它看起来像什么?
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.
以下示例制作了一个由3个字母组成的单词的平铺图像,我认为这与您的问题一致。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.