我正在创建一个小脚本来自动将文件从 .jpg 转换为 .png:
import glob, os
import Image
basedir = "C:\Users\User\Desktop\\fimg\images\\"
prefix = "picture"
def convert_png():
for jpg in glob.glob(os.path.join(basedir, '*.jpg')):
filename = f.rsplit('.')[-2]
njpg = Image.open(basedir + '%s.jpg' % (filename))
njpg.save(basedir + '%s.png' % (filename))
print "CONVERTED: " + filename + '.png'
for i, f in enumerate(os.listdir(basedir), 1):
convert_png()
但是有了这个,它似乎会多次转换文件(大约 10 次),因为它会打印这个数量的转换后的消息,然后再移动到下一个文件。这里有什么问题导致它这样做吗?
C:\Users\User\Desktop\fimg>python test.py
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 16094.png
CONVERTED: 185500.png
CONVERTED: 185500.png
CONVERTED: 185500.png
CONVERTED: 185500.png
CONVERTED: 185500.png
CONVERTED: 185500.png
CONVERTED: 185500.png
CONVERTED: 185500.png
CONVERTED: 185500.png
CONVERTED: 185500.png