想要在 Python 的另一行中保留两张图片的其余部分

Want to keep rest of two images in another row in Python

我有五张(动态获取的)图片,我将所有五张图片水平显示为一张,但外观不太好,下面是输出的代码和 SS:

def merge_images(file_list, width, height): 

imgs = [Image.open(x) for x in file_list]

if '{0}x{1}'.format (width, height) in HORIZONTAL_ALIGNMENT: 
    total width = width len(file List) '
    max_height = height

else:
    total width = width
    max_height = height * len (file_list)

new_img = Image.new('RGB', (total_width, max_height)) '
x_offset, y_offset = 0, 0

for img in imgs:
    img = img.resize((258, 125)) new_img.paste(img, (x_offset, y_offset))

    x_offset += img.size[0] + 10 
  # y_offset += img.size[1]

new_img.save("test.jpg)

输出:

预期:第一行只需要三张图片,下一行只需要两张图片。你能帮我修改我的代码吗?

非常感谢您的回复。

如果所有图片都具有相同的高度,您可以这样做:

x_offset, y_offset = 0, 0
img_counter = 0 

for img in imgs:
   img = img.resize((258, 125)) new_img.paste(img, (x_offset, y_offset))

   x_offset += img.size[0] + 10

   img_counter += 1
   if img_counter == 3:
       img_counter = 0
       x offset = 0
       y_offset += img.size[1]

您还可以检查 img_counter 是否是 3 的倍数以稍微清理代码,但这会很好