linux下使用imagemagick批量生成缩略图的python脚本。程序用了递归,可以查找目录下所有的图片按照一定的规则生成指定宽度的缩略图。
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#批量resize当前目录下的图片,linux测试过。
#使用imagemagick
import os, sys
iswindows = 'win32' in sys.platform.lower() or 'win64' in sys.platform.lower()
isosx = 'darwin' in sys.platform.lower()
def convert(dirname, size='400*400'):
for filename in os.listdir(dirname):
if "thumbs" in filename or "cache" in filename:
continue
filename = os.path.join(dirname, filename)
if os.path.isdir(filename):
convert(filename, size)
elif filename.lower().endswith("jpg"):
tname=filename.rsplit('/',1)
thumb_filename= "%s%s%s" % (tname[0],"/thumbs/thumbs_",tname[1])
cmd = "/usr/local/imagemagick/bin/convert \"%s\" -resize %s \"%s\"" % (filename, size, thumb_filename)
print "process.", filename
#print cmd
os.system(cmd)
if __name__ == "__main__":
if len(sys.argv) >= 2:
size = sys.argv[1]
else:
size = "400"
#convert 当前目录下的所有图片
convert('.', size)