Private
Public Access
1
0

image upload working

This commit is contained in:
Sander Roosendaal
2018-01-23 14:37:52 +01:00
parent 983b3be86c
commit 22cba46f3b
6 changed files with 395 additions and 9 deletions

View File

@@ -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")