Yesterday I came back from my holidays in Korea with a whole bunch of pictures which I now want to upload to my website to share them with my friends. Usually I take high resolution pictures with a size of 3056×1334 pixel. That’s of course to big for publishing them in the internet, so I wrote a small python script to convert them to 800×600 automatically by using the Unix convert command:
import sys import commands def convert(file, resize="800x600", prefix="small"): try: cmd = "convert -resize " + resize + " " + file + " " + prefix + "_" + file commands.getstatusoutput(cmd) except e: print e def usage(prog): print "Usage:", prog, "-r RESIZE -p PREFIX FILES..." if __name__ == "__main__": argv = sys.argv[1:] if len(argv) <= 1: usage(sys.argv) sys.exit(-1) resize = "800x600" prefix = "small" for i in range(len(argv)): arg = argv[i] if arg == "-r" and i+1 < len(argv): resize = argv[i+1] elif arg == "-p" and i+1 < len(argv): prefix = argv[i+1] elif argv[i-1] != "-r" or argv[i-1] != "-p": convert(arg, resize, prefix)
- -*- coding: utf-8 -*-
Run the script with convert.py *.jpg and it will convert all jpg file into 800×600 pixel images while preserving the original file and creating new ones named “small_ORIGINALFILENAME”. Use the parameters “-r” and “-p” to define the resize rate or to change the prefix, see man convert for more details.