0

I feel like this question was answered many times before, but i can't find anything.

I have a script which takes a text as command line argument and then generates images with the text in it. It generates image for every word enterd in to the command line but I don't know how to save them. I want to save them like this; first image would be saved as 0001.png, the second one as 0002.png and so on, how can I do this?

Daburu
  • 109
  • 1
  • 11
  • @Carcigenicate I really just don't know how to generate the string in this format (0001) and how to then combine it with ".png" to form a valid file name – Daburu Nov 20 '19 at 17:45
  • Which part are you having trouble with? Do you need help figuring out how to save an image to a file? Or do you need help with generating the file names? – Code-Apprentice Nov 20 '19 at 17:49
  • For the leading zeros, look at `str.format()`. Or google "python leading zeros". – Code-Apprentice Nov 20 '19 at 17:49
  • Does this answer your question? [Display number with leading zeros](https://stackoverflow.com/questions/134934/display-number-with-leading-zeros) – Code-Apprentice Nov 20 '19 at 18:53

2 Answers2

6

Use f-strings, you can specify how many 0's the digit should have at minimum:

for i in range(1, 1200, 103): # Just to generate bigger numbers
    print(f"{i:04d}.png")

Output:

0001.png
0104.png
0207.png
0310.png
0413.png
0516.png
0619.png
0722.png
0825.png
0928.png
1031.png
1134.png
1

You could try zfill method.

Example:

n = 10
EXTN = ".png"

filename = str(n).zfill(4) + EXTN

Here we put 4 if you want 4 digits in the filename.

Output:

0010.png
praneeth
  • 324
  • 2
  • 8