Nishadh KA

Scale images using python script

2013-09-10


For scaling jpeg image files in bulk, there is a python script from this link.

http://united-coders.com/christian-harms/image-resizing-tips-every-coder-should-know/

import Image, os, sys
for filename in sys.argv[1:]:
    img = Image.open(filename).resize( (200,200) )
    out = file(os.path.splitext(filename)[0]+"_thumb.jpg", "w")
    try:
        img.save(out, "JPEG")
    finally:
        out.close()

It is only some eight lines of code, but doing a tremendous job. Another essential thing to learn is the use of finding and regex to feed the files into a python script. To find and python feeder code to make the script work on all the files inside a directory.

find . -regex .*jpg | xargs python resize.py