-2

I am saving images with count function like below

count = 0
new_im.save("C:/Users/"+str(count)+".jpg", "JPEG", quality=100, optimize=True, progressive=True)
count += 1

The images' names will be 1.jpg, 2.jpg, 3.jpg, etc.

How to make it save as 001.jpg, 002.jpg, 003.jpg?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Guanlin Chen
  • 59
  • 1
  • 9
  • 1
    Does this answer your question? [Display number with leading zeros](https://stackoverflow.com/questions/134934/display-number-with-leading-zeros) – Random Davis Oct 15 '20 at 22:54

1 Answers1

1
count = 0
new_im.save("C:/Users/"+'{:0>3}'.format(count)+".jpg", "JPEG", quality=100, optimize=True, progressive=True)
count += 1

I have chosen the above so that it looks as similar to your code as possible. But as Gino Mempin has pointed out in the comments there are nicer ways to write it. He suggested

"C:/Users/{:0>3}.jpg".format(count)

I think I would even prefer using f strings like so:

f"C:/Users/{count:0>3}.jpg"
Lukas S
  • 3,212
  • 2
  • 13
  • 25