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
|
bgneal@696
|
15
|
bgneal@696
|
16 logger = logging.getLogger(__name__)
|
bgneal@696
|
17
|
bgneal@696
|
18
|
bgneal@697
|
19 def process_file(f, user, bucket):
|
bgneal@696
|
20 """Perform processing on the given uploaded image file:
|
bgneal@696
|
21
|
bgneal@696
|
22 * The image is resized if necessary
|
bgneal@696
|
23 * A thumbnail version is created
|
bgneal@696
|
24 * The image and thumbnail are uploaded to an S3 bucket
|
bgneal@696
|
25 * The image and thumbnail URLs are returned as a tuple
|
bgneal@696
|
26
|
bgneal@696
|
27 """
|
bgneal@696
|
28 logger.info('Processing image file for {}: {}'.format(user.username, f.name))
|
bgneal@696
|
29
|
bgneal@696
|
30 unique_key = uuid.uuid4().hex
|
bgneal@696
|
31 ext = os.path.splitext(f.name)[1]
|
bgneal@696
|
32 filename = '/tmp/' + unique_key + ext
|
bgneal@696
|
33 with open(filename, 'wb') as fp:
|
bgneal@696
|
34 for chunk in f.chunks():
|
bgneal@696
|
35 fp.write(chunk)
|
bgneal@696
|
36
|
bgneal@696
|
37 # Resize image if necessary
|
bgneal@696
|
38 image = Image.open(filename)
|
bgneal@696
|
39 if image.size > settings.USER_PHOTOS_MAX_SIZE:
|
bgneal@696
|
40 logger.debug('Resizing from {} to {}'.format(image.size, settings.USER_PHOTOS_MAX_SIZE))
|
bgneal@696
|
41 image.thumbnail(settings.USER_PHOTOS_MAX_SIZE, Image.ANTIALIAS)
|
bgneal@696
|
42 image.save(filename)
|
bgneal@696
|
43
|
bgneal@696
|
44 # Create thumbnail
|
bgneal@696
|
45 logger.debug('Creating thumbnail')
|
bgneal@696
|
46 image = Image.open(filename)
|
bgneal@696
|
47 image.thumbnail(settings.USER_PHOTOS_THUMB_SIZE, Image.ANTIALIAS)
|
bgneal@696
|
48 thumb = BytesIO()
|
bgneal@696
|
49 image.save(thumb, format=image.format)
|
bgneal@696
|
50
|
bgneal@696
|
51 # Upload both images to S3
|
bgneal@697
|
52 logging.debug('Uploading image')
|
bgneal@696
|
53 now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
bgneal@697
|
54 metadata = {'user': user.username, 'date': now}
|
bgneal@697
|
55 file_key = unique_key + ext
|
bgneal@697
|
56 bucket.upload_from_filename(file_key, filename, metadata)
|
bgneal@696
|
57
|
bgneal@696
|
58 logging.debug('Uploading thumbnail')
|
bgneal@697
|
59 thumb_key = '{}t{}'.format(unique_key, ext)
|
bgneal@697
|
60 bucket.upload_from_string(thumb_key, thumb.getvalue())
|
bgneal@696
|
61
|
bgneal@696
|
62 os.remove(filename)
|
bgneal@696
|
63
|
bgneal@696
|
64 logger.info('Completed processing image file for {}: {}'.format(user.username, f.name))
|
bgneal@696
|
65
|
bgneal@696
|
66 url_base = '{}/{}/'.format(settings.USER_PHOTOS_BASE_URL,
|
bgneal@696
|
67 settings.USER_PHOTOS_BUCKET)
|
bgneal@696
|
68
|
bgneal@697
|
69 return (url_base + file_key, url_base + thumb_key)
|