【问题标题】:How to name files with a count when saving?保存时如何用计数命名文件?
【发布时间】:2020-10-15 22:51:54
【问题描述】:

我正在使用如下计数功能保存图像

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

图像的名称将是 1.jpg、2.jpg、3.jpg 等。

如何使其另存为001.jpg、002.jpg、003.jpg?

【问题讨论】:

标签: python save


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

我选择了上面的代码,以便它看起来与您的代码尽可能相似。但正如 Gino Mempin 在 cmets 中指出的那样,有更好的方法来编写它。他建议

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

我想我什至更喜欢像这样使用 f 字符串

f"C:/Users/{count:0>3}.jpg"

【讨论】:

    猜你喜欢
    • 2018-04-23
    • 1970-01-01
    • 2017-07-07
    • 2023-04-04
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    相关资源
    最近更新 更多