如何实现多次运行图片生成脚本并以递增序列命名保存输出文件
Solution to Generate Numbered Unique Images
Hey there! I see you want to run your script multiple times to generate unique images with incrementing filenames—let's get this sorted for you. The key is to wrap your entire image-generation logic in a loop, and dynamically adjust the filename each time to include the running number.
Here's the modified code that will generate 25 images (you can change the number by adjusting the range value) with filenames like final_img01.png up to final_img25.png:
from math import floor from PIL import Image from random import randint # Set how many images you want to generate N = 25 # Loop N times to create each unique image for img_num in range(N): # Read all images (we can keep this inside or outside the loop; inside ensures fresh reads each time if files change) 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 with incrementing two-digit number (e.g., 01, 02...25) filename = f'final_img{str(img_num + 1).zfill(2)}.png' final_img.save(filename) print(f"Saved {filename}") # Optional: Show the last generated image if you want # final_img.show()
Key Changes Explained:
- Added a top-level loop with
range(N)whereNis the number of images you want to generate. This runs the entire image creation process N times. - Used
str(img_num + 1).zfill(2)to format the number as a two-digit string (so 1 becomes "01", 2 becomes "02", etc.)—this keeps your filenames neat and ordered. - Moved the image loading inside the loop (though you could keep it outside if your source images don't change between runs; putting it inside is safer if you might modify them mid-process).
- Added a print statement to track which files are being saved, which helps with debugging.
Quick Notes:
- If you want more or fewer than 25 images, just change the value of
N. - Make sure your
background2.png,orangedot.png, andwhiteX.pngare in the same directory as the script, or provide the full file path. - If you don't need the two-digit numbering, you can simplify the filename to
f'final_img{img_num + 1}.png'to getfinal_img1.png,final_img2.png, etc.
内容的提问来源于stack exchange,提问作者Albert
相关产品推荐
相关产品推荐

