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