#!/usr/bin/env python3

import glob
import argparse
import shutil

import numpy as np
import scipy.misc

from .get_images import save_images
from . import SAVE_DIR

WIDTH = 500
HEIGHT = 500
EXTENSION = "jpg"
MAX_INTENSITY = 255


def average_images(filenames):
    images = []
    for filename in filenames:
        try:
            images.append(scipy.misc.imread(filename, mode="RGB"))
        # Some images are corrupted
        except OSError:
            pass
    resized_images = resize_images(images)
    weights = np.random.dirichlet(np.ones(len(resized_images)))
    return np.average(resized_images, axis=0, weights=weights)


def resize_images(images):
    return [scipy.misc.imresize(img, (WIDTH, HEIGHT)) for img in images if img.shape]


def save_image(new_image, args):
    filename = '{output_filename}.{ext}'.format(
            output_filename = '_'.join(args.terms.split()),
            ext=EXTENSION)
    scipy.misc.imsave(filename, new_image)
    return filename


def get_args():
    parser = argparse.ArgumentParser(
        description="Average multiple images")
    parser.add_argument(
            "terms", type=str,
            help="Keywords for images to search for and filename")
    parser.add_argument(
        '--count', '-c', type=int,
        default=10, help='number of photos to be combined')
    parser.add_argument(
        '--dir', '-d', type=str,
        help='dir to fetch images from')
    parser.add_argument(
        '--offset', '-o', type=int,
        default=0, help='odd a value to all pixels')

    return parser.parse_args()


def offset_image(image, offset):
    return np.clip(image + offset, 0, MAX_INTENSITY)

def delete_images():
    shutil.rmtree(SAVE_DIR)

def main():
    args = get_args()
    if args.dir:
        filenames = glob.glob(args.dir + "*")
    else:
        filenames = save_images(args.terms, args.count)
    new_image = average_images(filenames)
    new_image = offset_image(new_image, args.offset)
    filename = save_image(new_image, args)
    delete_images()
    print("{} saved in current directory.".format(filename))

if __name__ == '__main__':
    main()

@@ -774,24 +774,81 @@
 age, args):%0A
+    output = args.output or '_'.join(args.terms.split())%0A
     filename
@@ -918,36 +918,14 @@
 e = 
-'_'.join(args.terms.split())
+output
 ,%0A  
@@ -1067,24 +1067,16 @@
 tParser(
-%0A       
  descrip
@@ -1104,16 +1104,475 @@
 e images
+%22)%0A    subparsers = parser.add_subparsers(dest='mode',%0A            help='Use images from a local dir or download new images')%0A    subparsers.required = True%0A%0A    parser_local = subparsers.add_parser('local', help='Directory to combine images from')%0A    parser_local.add_argument( 'dir', type=str,%0A        help='dir to fetch images from')%0A%0A    parser_download = subparsers.add_parser('download',%0A            help=%22Keywords for images to search for and filename
 %22)%0A    p
@@ -1568,32 +1568,41 @@
 ame%22)%0A    parser
+_download
 .add_argument(%0A 
@@ -1700,32 +1700,41 @@
 ame%22)%0A    parser
+_download
 .add_argument(%0A 
@@ -1829,32 +1829,41 @@
 ned')%0A    parser
+_download
 .add_argument(%0A 
@@ -1876,25 +1876,28 @@
  '--
-dir
+offset
 ', '-
-d
+o
 ', type=
 str,
@@ -1884,35 +1884,35 @@
 et', '-o', type=
-str
+int
 ,%0A        help='
@@ -1909,41 +1909,54 @@
     
-help='dir to fetch images from
+default=0, help='odd a value to all pixels
 ')%0A
+%0A
     
@@ -1980,36 +1980,36 @@
 nt(%0A        '--o
-ffse
+utpu
 t', '-o', type=i
@@ -1999,35 +1999,35 @@
 ut', '-o', type=
-int
+str
 ,%0A        defaul
@@ -2024,50 +2024,41 @@
     
-default=0, help='odd a value to all pixels
+help='file to output the image to
 ')%0A%0A
@@ -2273,19 +2273,31 @@
 if args.
-dir
+mode == 'local'
 :%0A      

Add subparsers