annotate user_photos/images.py @ 696:b2a8fde3173a

Got the image resizing and uploading working. It needs a lot of work though. This commit is just to capture something that works.
author Brian Neal <bgneal@gmail.com>
date Sun, 08 Sep 2013 19:06:54 -0500
parents
children 67f8d49a9377
rev   line source
bgneal@696 1 """images.py
bgneal@696 2
bgneal@696 3 This module contains image processing routines for the user_photos application.
bgneal@696 4
bgneal@696 5 """
bgneal@696 6 import datetime
bgneal@696 7 import logging
bgneal@696 8 from io import BytesIO
bgneal@696 9 import os.path
bgneal@696 10 import uuid
bgneal@696 11
bgneal@696 12 from django.conf import settings
bgneal@696 13 from PIL import Image
bgneal@696 14 from boto.s3.connection import S3Connection
bgneal@696 15 from boto.s3.key import Key
bgneal@696 16
bgneal@696 17
bgneal@696 18 logger = logging.getLogger(__name__)
bgneal@696 19
bgneal@696 20
bgneal@696 21 def process_file(f, user):
bgneal@696 22 """Perform processing on the given uploaded image file:
bgneal@696 23
bgneal@696 24 * The image is resized if necessary
bgneal@696 25 * A thumbnail version is created
bgneal@696 26 * The image and thumbnail are uploaded to an S3 bucket
bgneal@696 27 * The image and thumbnail URLs are returned as a tuple
bgneal@696 28
bgneal@696 29 """
bgneal@696 30 logger.info('Processing image file for {}: {}'.format(user.username, f.name))
bgneal@696 31
bgneal@696 32 unique_key = uuid.uuid4().hex
bgneal@696 33 ext = os.path.splitext(f.name)[1]
bgneal@696 34 filename = '/tmp/' + unique_key + ext
bgneal@696 35 with open(filename, 'wb') as fp:
bgneal@696 36 for chunk in f.chunks():
bgneal@696 37 fp.write(chunk)
bgneal@696 38
bgneal@696 39 # Resize image if necessary
bgneal@696 40 image = Image.open(filename)
bgneal@696 41 if image.size > settings.USER_PHOTOS_MAX_SIZE:
bgneal@696 42 logger.debug('Resizing from {} to {}'.format(image.size, settings.USER_PHOTOS_MAX_SIZE))
bgneal@696 43 image.thumbnail(settings.USER_PHOTOS_MAX_SIZE, Image.ANTIALIAS)
bgneal@696 44 image.save(filename)
bgneal@696 45
bgneal@696 46 # Create thumbnail
bgneal@696 47 logger.debug('Creating thumbnail')
bgneal@696 48 image = Image.open(filename)
bgneal@696 49 image.thumbnail(settings.USER_PHOTOS_THUMB_SIZE, Image.ANTIALIAS)
bgneal@696 50 thumb = BytesIO()
bgneal@696 51 image.save(thumb, format=image.format)
bgneal@696 52
bgneal@696 53 # Upload both images to S3
bgneal@696 54 logger.debug('Getting connection / bucket')
bgneal@696 55 conn = S3Connection(settings.USER_PHOTOS_ACCESS_KEY,
bgneal@696 56 settings.USER_PHOTOS_SECRET_KEY)
bgneal@696 57 bucket = conn.get_bucket(settings.USER_PHOTOS_BUCKET, validate=False)
bgneal@696 58
bgneal@696 59 now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
bgneal@696 60
bgneal@696 61 logging.debug('Uploading image')
bgneal@696 62 k1 = Key(bucket)
bgneal@696 63 k1.key = unique_key + ext
bgneal@696 64 k1.set_metadata('user', user.username)
bgneal@696 65 k1.set_metadata('date', now)
bgneal@696 66 k1.set_contents_from_filename(filename)
bgneal@696 67
bgneal@696 68 logging.debug('Uploading thumbnail')
bgneal@696 69 k2 = Key(bucket)
bgneal@696 70 k2.key = '{}t{}'.format(unique_key, ext)
bgneal@696 71 k2.set_metadata('user', user.username)
bgneal@696 72 k2.set_metadata('date', now)
bgneal@696 73 k2.set_contents_from_string(thumb.getvalue())
bgneal@696 74
bgneal@696 75 logging.debug('Making public')
bgneal@696 76 k1.make_public()
bgneal@696 77 k2.make_public()
bgneal@696 78
bgneal@696 79 os.remove(filename)
bgneal@696 80
bgneal@696 81 logger.info('Completed processing image file for {}: {}'.format(user.username, f.name))
bgneal@696 82
bgneal@696 83 url_base = '{}/{}/'.format(settings.USER_PHOTOS_BASE_URL,
bgneal@696 84 settings.USER_PHOTOS_BUCKET)
bgneal@696 85
bgneal@696 86 return (url_base + k1.key, url_base + k2.key)