Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save akiraak/621894 to your computer and use it in GitHub Desktop.
Save akiraak/621894 to your computer and use it in GitHub Desktop.
resize.py --width=320 --height=480 --fileoder=true --output=./out_dir src_dir/*
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
from __future__ import division
import sys, os
import math
import re
from commands import getstatusoutput
from optparse import OptionParser, OptionGroup
def execute(command):
status, output = 0, ''
print command
status, output = getstatusoutput(command)
print output
if not status == 0:
sys.exit(status)
return status
def imformat(program, srcs, dst, params):
command = "%s %s " % (program, ' '.join(srcs))
for key, val in params:
command += "-%s %s " % (key, val)
command += dst
return command
def convertize(src, dst, params):
return imformat('convert', [src], dst, params)
def resize_command(file, dst, resizeWidth, resizeHeight):
params = [('resize', "%dx%d!"%(resizeWidth, resizeHeight))]
return convertize(file, dst, params)
def resize(files, options):
if options.width is None or options.width <= 0 or options.height is None or options.height <= 0 or options.output is None:
print "--width --height --output を正しく設定してください。"
sys.exit(1)
i = 0
for file in files:
i = i + 1
root, ext = os.path.splitext(file)
if ext == ".jpg" or ext == ".JPG" or ext == ".png" or ext == ".PNG":
file_name = ''
if options.fileoder is not None and options.fileoder == 'true':
file_name = '%03d'%i + ext
else:
file_name = os.path.basename(file)
output_file = options.output + '/' + file_name
command = resize_command(file, output_file, options.width, options.height)
execute(command)
if __name__ == '__main__':
usage = "usage: %prog [options] [resize]"
p = OptionParser(usage=usage, conflict_handler="resolve")
resize_group = OptionGroup(p, "resize options")
resize_group.add_option("--width", dest="width", action="store", type="int", default=None)
resize_group.add_option("--height", dest="height", action="store", type="int", default=None)
resize_group.add_option("--fileoder", dest="fileoder", action="store", default=None)
resize_group.add_option("--output", dest="output", action="store", default=None)
p.add_option_group(resize_group)
(options, args) = p.parse_args()
def die():
print p.print_help()
sys.exit(1)
if not len(args) > 0:
die()
files = args[0:]
print files, options
resize(files, options)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment