繁体   English   中英

如何将 output 保存为图像文件多次,名称中包含递增的数字序列(name01、name02 等)?

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

我有一个脚本,每次运行时 output 的图像略有不同。 现在,我尝试使用for 循环rangexrange函数多次运行它,但均未成功。 我的目标是保存图像文件 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()

这会起作用,你必须输入你想要的图像数量,它会将图像保存为"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)

“枚举”会很好。

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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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