Created
April 15, 2012 16:43
-
-
Save fireball2018/2393781 to your computer and use it in GitHub Desktop.
批量处理当前目录及子目录图片缩略图,使用imagemagick
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding:utf-8 -*- | |
| 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='640x640'): | |
| for filename in os.listdir(dirname): | |
| if filename == "thumbs": | |
| continue | |
| filename = os.path.join(dirname, filename) | |
| if os.path.isdir(filename): | |
| convert(filename, size) | |
| elif filename.lower().endswith("jpg"): | |
| thumb_filename = os.path.join("thumbs", filename) | |
| thumb_dirname = os.path.join("thumbs", dirname) | |
| if not os.path.isdir(thumb_dirname): | |
| os.makedirs(thumb_dirname) | |
| if iswindows: | |
| cmd = "convert.exe %s -resize %s %s" % (filename, size, thumb_filename) | |
| print u"处理:".encode('cp936'), filename | |
| else: | |
| cmd = "convert %s -resize %s %s" % (filename, size, thumb_filename) | |
| print "处理:", filename | |
| os.system(cmd) | |
| if __name__ == "__main__": | |
| if len(sys.argv) >= 2: | |
| size = sys.argv[1] | |
| else: | |
| size = "640x640" | |
| convert('.', size) | |
| if iswindows: | |
| raw_input(u"处理完成,按任意键退出...".encode('cp936')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment