from PIL import Image
import StringIO
import random
import os

from cache import cache


def create_image(width, height):
    stringfile = StringIO.StringIO()
    im = Image.open(choose_image(width, height))
    im.thumbnail((int(width), int(height)), Image.ANTIALIAS)
    im = im.crop((0, 0, int(width), int(height)))
    im.save(stringfile, 'JPEG')
    return stringfile


def is_portrait(width, height):
    return height > width


def is_square(width, height):
    return width == height


def is_landscape(width, height):
    return height < width


def list_squares():
    return list_files("puppies/squares")


def list_portraits():
    return list_files("puppies/portraits")


def list_landscapes():
    return list_files("puppies/landscapes")


@cache.memoize()
def list_files(path):
    images = []
    for filename in os.listdir(path):
        if filename.endswith(".jpg"):
            images.append(path + "/" + filename)
    return images


def choose_image(width, height):
    if is_portrait(width, height):
        images = list_portraits()
    elif is_square(width, height):
        images = list_squares()
    elif is_landscape(width, height):
        images = list_landscapes()
    return random.choice(images)

@@ -364,24 +364,301 @@
 tringfile%0A%0A%0A
+def choose_image(width, height):%0A    if is_portrait(width, height):%0A        images = list_portraits()%0A    elif is_square(width, height):%0A        images = list_squares()%0A    elif is_landscape(width, height):%0A        images = list_landscapes()%0A    return random.choice(images)%0A%0A%0A
 def is_portr
@@ -1226,281 +1226,4 @@
 ges%0A
-%0A%0Adef choose_image(width, height):%0A    if is_portrait(width, height):%0A        images = list_portraits()%0A    elif is_square(width, height):%0A        images = list_squares()%0A    elif is_landscape(width, height):%0A        images = list_landscapes()%0A    return random.choice(images)%0A

refactor image_helper