#!/usr/bin/env python3

import boto
import config
import hashlib
from boto.s3.key import Key
from flask import Flask, abort, jsonify, request, send_file
from wand.image import Color, Image

app = Flask(__name__)
app.config.from_object(config)


def upload(image):
    s3 = boto.connect_s3()
    bucket = s3.get_bucket(app.config['AWS_BUCKET_NAME'])
    blob = image.make_blob()

    h = hashlib.md5()

    # generate filename from 32 random letters
    # filename = ''.join(random.sample(string.ascii_letters, 32))

    # generate filename from image md5
    h.update(blob)
    filename = h.hexdigest()

    key = Key(bucket)

    # key key is image filename, data is blob and headers
    key.key = '%s.%s' % (filename, image.format.lower())
    key.set_contents_from_string(blob, headers={
        'Content-Type': image.mimetype
        })
    key.make_public()
    return key


@app.route('/blend', methods=['POST', 'OPTIONS'])
def blend():
    picture, flag = request.files['picture'], request.files['flag']

    # don't be foolish. please send stuff.
    if not picture or not flag:
        abort(400)

    with Image(file=flag) as foreground:
        with Image(height=foreground.height, width=foreground.width,
                   background=Color('gray60')) as alpha:
            # this will copy the opacity of a 60% opacity image to foreground
            foreground.composite_channel('default_channels', alpha,
                                         'copy_opacity', left=0, top=0)

        with Image(file=picture) as background:
            # rescale flag to be the same size as background
            foreground.resize(height=background.height,
                              width=background.width)

            # blend both images into one
            background.composite_channel('default_channels', foreground,
                                         'blend', left=0, top=0)

            # upload to S3 converted to PNG
            key = upload(background.convert('png'))

    # return json object with url to newly uploaded file
    response = jsonify(url=key.generate_url(app.config['AWS_EXPIRES_IN']))
    response.headers.add('Access-Control-Allow-Origin',
                         app.config['ALLOWED_ORIGIN'])
    response.headers.add('Access-Control-Allow-Credentials',
                         'true')
    return response


if __name__ == '__main__':
    app.run()

@@ -58,16 +58,44 @@
 hashlib%0A
+import random%0Aimport string%0A
 from bot
@@ -404,31 +404,8 @@
 ()%0A%0A
-    h = hashlib.md5()%0A%0A
     
@@ -429,18 +429,17 @@
 me from 
-32
+8
  random 
@@ -449,18 +449,16 @@
 ters%0A   
- #
  filenam
@@ -509,10 +509,9 @@
 rs, 
-32
+8
 ))%0A%0A
@@ -552,16 +552,42 @@
  md5%0A   
+ # h = hashlib.md5()%0A    #
  h.updat
@@ -593,24 +593,26 @@
 te(blob)%0A   
+ #
  filename = 

Revert to random name instead of keep file name