image upload working
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import time
|
||||
import gzip
|
||||
import shutil
|
||||
|
||||
import hashlib
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
def format_pace_tick(x,pos=None):
|
||||
min=int(x/60)
|
||||
min=int(x/60)
|
||||
sec=int(x-min*60.)
|
||||
sec_str=str(sec).zfill(2)
|
||||
template='%d:%s'
|
||||
@@ -45,6 +45,14 @@ def format_time(x,pos=None):
|
||||
|
||||
return str1
|
||||
|
||||
def validate_image_extension(value):
|
||||
import os
|
||||
ext = os.path.splitext(value.name)[1].lower()
|
||||
valid_extension = ['.jpg','.jpeg','.png','.gif']
|
||||
|
||||
if not ext in valid_extension:
|
||||
raise ValidationError(u'File not supported')
|
||||
|
||||
def validate_file_extension(value):
|
||||
import os
|
||||
ext = os.path.splitext(value.name)[1]
|
||||
@@ -62,6 +70,34 @@ def must_be_csv(value):
|
||||
raise ValidationError(u'File not supported!')
|
||||
|
||||
|
||||
def handle_uploaded_image(i):
|
||||
import StringIO
|
||||
from PIL import Image, ImageOps
|
||||
import os
|
||||
from django.core.files import File
|
||||
image_str = ""
|
||||
for c in i.chunks():
|
||||
image_str += c
|
||||
imagefile = StringIO.StringIO(image_str)
|
||||
image = Image.open(imagefile)
|
||||
|
||||
if image.mode not in ("L", "RGB"):
|
||||
image = image.convert("RGB")
|
||||
|
||||
basewidth = 600
|
||||
wpercent = (basewidth/float(image.size[0]))
|
||||
hsize = int((float(image.size[1])*float(wpercent)))
|
||||
image = image.resize((basewidth,hsize), Image.ANTIALIAS)
|
||||
|
||||
|
||||
|
||||
filename = hashlib.md5(imagefile.getvalue()).hexdigest()+'.jpg'
|
||||
|
||||
filename2 = os.path.join('static/plots/',filename)
|
||||
image.save(filename2,'JPEG')
|
||||
return filename,filename2
|
||||
|
||||
|
||||
def handle_uploaded_file(f):
|
||||
fname = f.name
|
||||
timestr = time.strftime("%Y%m%d-%H%M%S")
|
||||
|
||||
Reference in New Issue
Block a user