понедельник, 9 сентября 2013 г.

Resize images with Python

Recently, I needed to resize some photos in order to upload them to the webpage. There was no point in uploading the full-sized photos, so I downscaled them with a ratio 1/3. The script should be started from the directory with your JPEGs. The output is a sequence of files: resized_0.jpg, resized_1.jpg, resized_2.jpg etc
from os import walk
import Image

def imgResize(im):
    div = 3
    width = im.size[0] / div
    height = im.size[1] / div

    im_result = im.resize((width, height), Image.ANTIALIAS)
    # best down-sizing filter

    return im_result


files = []
for (dirpath, dirnames, filenames) in walk("."):
    files.extend(filenames)
    break

for i, f in enumerate(files):
    if "JPG" in f and not "resized" in f:
        im = Image.open(f)
        im = imgResize(im)
        im.save("resized_"+str(i)+".jpg")

Комментариев нет:

Отправить комментарий