如何将输出多次保存为名称中带有递增数字序列(name01、name02 等)的图像文件?

How to save the output as image file mutliple times with incremental number sequence in the name (name01,name02,etc.)?

我有一个脚本,每次 运行 时输出的图像都略有不同。现在我已经尝试使用 for loopsrangexrange[=16= 多次 运行 ] 功能没有成功。我的目标是将图像文件保存 N 次(例如 25 次),并在名称上附加一个递增数字(例如“filename01.png、filename02.png 等,直到 filename025.png)。 (请注意,我已经从代码中删除了之前使用上述函数的任何尝试,以免弄乱工作脚本) 这是代码:

from math import floor
from PIL import Image
from random import randint

# Read all images
bckgnd = Image.open('background2.png').convert('RGBA')
orangedot = Image.open('orangedot.png')
whiteX = Image.open('whiteX.png')

# Width and height of each "tile"
w, h = bckgnd.size

# Calculate number of tiles for x and y direction by A4 paper size
# (21 cm x 29.7 cm), and some resolution like dpi = 300
n_tiles = (floor((21.0 / 2.54 * 300) / w), floor((29.7 / 2.54 * 300) / h))

# Prepare final image of sufficient size
final_img = Image.new('RGBA', (n_tiles[0] * w, n_tiles[1] * h), color=(0, 0, 0, 0))

# Iterate all tiles
for i_x in range(n_tiles[0]):
    for i_y in range(n_tiles[1]):

        # Upper left (x, y) coordinates of current tile
        x, y = i_x * w, i_y * h

        # 1st: Paste background to current tile
        final_img.paste(bckgnd, (x, y), mask=bckgnd)

        # 2nd: Randomly generate location of orange dot and paste to current tile
        od_x, od_y = randint(30, 170), randint(0, 100)
        final_img.paste(orangedot, (x + od_x, y + od_y), mask=orangedot)

        # 3rd: Paste white X to current tile
        final_img.paste(whiteX, (x, y), mask=whiteX)

# Save and show final image
final_img.save('final_img.png')
final_img.show()

'enumerate'就好了

for ind_x, i_x in enumerate(range(n_tiles[0])):
    for ind_y, i_y in enumerate(range(n_tiles[1])):

        # Upper left (x, y) coordinates of current tile
        x, y = i_x * w, i_y * h

        # 1st: Paste background to current tile
        final_img.paste(bckgnd, (x, y), mask=bckgnd)

        # 2nd: Randomly generate location of orange dot and paste to current tile
        od_x, od_y = randint(30, 170), randint(0, 100)
        final_img.paste(orangedot, (x + od_x, y + od_y), mask=orangedot)

        # 3rd: Paste white X to current tile
        final_img.paste(whiteX, (x, y), mask=whiteX)

        # save to img file
        final_img.save('final_img' + str(ind_x) + str(ind_y) + '.png')

这可行,您必须输入想要的图像数量,它会将图像保存为 "filename01,filename02...."。只需要 for 循环计数器字符串格式

from math import floor
from PIL import Image
from random import randint

# Read all images
c=1
times = int(input("How many times to save"))
for i in range(times):
  bckgnd = Image.open('background2.png').convert('RGBA')
  orangedot = Image.open('orangedot.png')
  whiteX = Image.open('whiteX.png')

  # Width and height of each "tile"
  w, h = bckgnd.size

  # Calculate number of tiles for x and y direction by A4 paper size
  # (21 cm x 29.7 cm), and some resolution like dpi = 300
  n_tiles = (floor((21.0 / 2.54 * 300) / w), floor((29.7 / 2.54 * 300) / h))

  # Prepare final image of sufficient size
  final_img = Image.new('RGBA', (n_tiles[0] * w, n_tiles[1] * h), color=(0, 0, 0, 0))

  # Iterate all tiles
  for i_x in range(n_tiles[0]):
      for i_y in range(n_tiles[1]):

          # Upper left (x, y) coordinates of current tile
          x, y = i_x * w, i_y * h

          # 1st: Paste background to current tile
          final_img.paste(bckgnd, (x, y), mask=bckgnd)

          # 2nd: Randomly generate location of orange dot and paste to current tile
          od_x, od_y = randint(30, 170), randint(0, 100)
          final_img.paste(orangedot, (x + od_x, y + od_y), mask=orangedot)

          # 3rd: Paste white X to current tile
          final_img.paste(whiteX, (x, y), mask=whiteX)

  # Save and show final image
  img_name = 'filename0{0}.png'.format(c)
  c+=1
  final_img.save(img_name)