-
-
Save venkatesh22/5d2b22f97956f45678c2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
from django.conf import settings | |
import S3, mimetypes | |
THUMBNAIL_SIZE = 70, 70 | |
MEDIUM_SIZE = 150, 150 | |
profile_image_path = settings.MEDIA_ROOT + '/images/roles/' | |
AWS_ACCESS_KEY_ID = 'YOUR_AWS_KEY' | |
AWS_SECRET_ACCESS_KEY = 'YOUR_AWS_SECRET' | |
BUCKET_NAME = settings.AMAZON_MEDIA_BUCKET | |
conn = S3.AWSAuthConnection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) | |
def handle_uploaded_picture(user_id, picture_file): | |
imo = Image.open(picture_file) | |
## First make the image a square. Crop it. | |
width, height = imo.size | |
if width > height: | |
delta = width - height | |
left = int(delta/2) | |
upper = 0 | |
right = height + left | |
lower = height | |
else: | |
delta = height - width | |
left = int(delta)/2 | |
upper = 0 | |
right = width | |
lower = width + upper | |
im = imo.crop((left, upper, right, lower)) | |
create_medium_size(im, user_id) | |
create_thumbnail_size(im, user_id) | |
def create_thumbnail_size(image_object, user_id): | |
image_object.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS) | |
image_object.save(profile_image_path + str(user_id) + "_70.jpg", "JPEG") | |
filedata = open(profile_image_path + str(user_id) + "_70.jpg", 'rb').read() | |
conn.put(BUCKET_NAME, 'images/provider/' + str(user_id)+'_70.jpg', S3.S3Object(filedata), {'x-amz-acl': 'public-read', 'Content-Type': 'image/jpeg'}) | |
def create_medium_size(image_object, user_id): | |
image_object.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS) | |
image_object.save(profile_image_path + str(user_id) + "_150.jpg", "JPEG") | |
filedata = open(profile_image_path + str(user_id) + "_150.jpg", 'rb').read() | |
conn.put(BUCKET_NAME, 'images/provider/' + str(user_id)+'_150.jpg', S3.S3Object(filedata), {'x-amz-acl': 'public-read', 'Content-Type': 'image/jpeg'}) | |
def delete_profile_picture_from_s3(user_id): | |
conn.delete(BUCKET_NAME, 'images/provider/' + str(user_id)+'_70.jpg') | |
conn.delete(BUCKET_NAME, 'images/provider/' + str(user_id)+'_150.jpg') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment