Created
March 31, 2015 13:07
-
-
Save lemoonbox/ca82f8f17460a47cde88 to your computer and use it in GitHub Desktop.
make thumnail befor save Image file
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
def handle_uploaded_image(i, x, y): | |
import StringIO | |
from PIL import Image, ImageOps | |
import os | |
from django.core.files.images import ImageFile | |
from django.core.files.uploadedfile import InMemoryUploadedFile | |
import uuid | |
IMAGE_TYPE=i.content_type | |
#PIL_TYPE use to save disk, FILE_EXTENSTION use to make the path | |
if IMAGE_TYPE =='image/jpeg': | |
PIL_TYPE = 'image/jpg' | |
PIL_SAVE = 'JPEG' | |
FILE_EXTENSION = 'jpg' | |
elif IMAGE_TYPE=='image/png': | |
PIL_TYPE = 'image/png' | |
PIL_SAVE = 'PNG' | |
FILE_EXTENSION = 'png' | |
else : | |
raise Exception('Image-type-error', "It's not jpeg of PNG") | |
#read image from InMemoryUploadedFile | |
image_str = "" | |
for c in i.chunks(): | |
image_str +=c | |
#create PIL image instance | |
imagefile = StringIO.StringIO(image_str) | |
image = Image.open(imagefile) | |
# if not RGB, convert | |
if image.mode not in ("L", "RGB"): | |
image = image.convert("RGB") | |
width, height = image.size | |
max_width = x | |
max_height = y | |
target_ratio = float(max_width)/float(max_height) | |
ratio = float(width)/float(height) | |
target_width = 0 | |
target_height = 0 | |
if (ratio>target_ratio): | |
target_width = max_width | |
target_height = int(target_width/ratio) | |
else: | |
target_height = max_height | |
target_width = int(ratio * target_height) | |
#resize(doing a thumb) | |
if target_width < width or target_height < height: | |
image = image.resize((target_width, target_height), Image.ANTIALIAS) | |
#re-initialize imageFile and set a hash (unique filename) | |
imagefile = StringIO.StringIO() | |
filename = str(uuid.uuid1())+"."+FILE_EXTENSION | |
#PIL instance(image) change to imagefile | |
#return FILE instance after just resize not save | |
thumnail_io= StringIO.StringIO() | |
image.save(thumnail_io, format=PIL_SAVE) | |
thumnail_io.seek(0) | |
content = InMemoryUploadedFile(thumnail_io, None, filename, PIL_TYPE, | |
thumnail_io.len, None) | |
return (filename, content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment