#!/usr/bin/env python

import py

TARGETDIR = '/home/suus'
IMGMAXSIZE = 600

def get_targetpath():
    dirpath = py.path.local(TARGETDIR).ensure(dir=True)
    dirname = py.std.time.strftime('images-%Y-%m-%d-%H-%M')
    return dirpath.join(dirname).ensure(dir=True)

def get_diskpath():
    path = py.path.local('/media/disk')
    return path

def convert_image(frompath, topath):
    image = py.std.Image.open(frompath.strpath)
    image.thumbnail((IMGMAXSIZE, IMGMAXSIZE), py.std.Image.ANTIALIAS)
    image.save(topath.strpath, 'JPEG')

if __name__ == '__main__':
    path = get_diskpath()
    target = get_targetpath()
    print 'going to copy images from the camera to disk'
    for subpath in path.visit():
        if not subpath.check(file=True, ext='.JPG'):
            continue
        print 'copying', subpath.basename
        subpath.copy(target)
    print 'going to convert images'
    for jpeg in target.listdir():
        if not jpeg.check(ext='.JPG'):
            continue
        print 'converting', jpeg.basename
        newname = jpeg.basename.lower()
        convert_image(jpeg, target.join(newname))
        print 'written to', newname


