resize photos before saving in report RDF

This commit is contained in:
2019-09-11 16:41:07 +02:00
parent 48cc4fc7d6
commit 74c41c4ca9
518 changed files with 63557 additions and 8 deletions

View File

@@ -21,6 +21,7 @@ from datetime import *
from sqlalchemy.exc import DBAPIError
from ..security import groupfinder
from PIL import Image
import os
import io
import shutil
@@ -351,6 +352,15 @@ def upload_doc(request):
@view_config(route_name='upload_img', renderer='../templates/dossier/upload_img.pt', permission='view')
def upload_img(request):
def photos_size(photos):
# calculer la taille totale des photos
sum_size = 0
for photo in photos :
# récupère la taille sans ' Ko'
sum_size += int(photo.taillefichier[:-3])
return '%s Mo' % round(sum_size / 1024, 1)
logged_in = request.authenticated_userid.upper()
norapport = request.matchdict['norapport']
@@ -381,7 +391,7 @@ def upload_img(request):
request.session.flash("Le format de ce fichier n'est pas valide. Téléchargement impossible.", 'warning')
else:
# récupère le nom du fichier et ajouter le no de rapport
filename = '%s-RDF%s-%s' % (societe, norapport, request.POST['filename'].filename)
filename = '%s-RDF%s-%s' % (societe, norapport, input_name)
# créer le répertoire du chantier s'il n'existe pas encore
path = '%s/%s/%s/%s' % (request.registry.settings['mondumas.devfac_dir'], societe, nochantier, norapport)
os.makedirs(path, exist_ok=True)
@@ -389,7 +399,7 @@ def upload_img(request):
file_path = os.path.join('%s/%s' % (path, filename))
# We first write to a temporary file to prevent incomplete files
temp_file_path = file_path + '~'
# Finally write the data to a temporary file
input_file.seek(0)
with open(temp_file_path, 'wb') as output_file:
@@ -401,9 +411,13 @@ def upload_img(request):
os.remove(temp_file_path)
request.session.flash("La taille de ce fichier dépasse la limite autorisée. Téléchargement impossible.", 'warning')
else:
# Now that we know the file has been fully saved to disk move it into place.
try:
os.rename(temp_file_path, file_path)
# using the Python Image Library (PIL) to resize an image
resize_photos(temp_file_path, file_path)
filesize = round(os.path.getsize(file_path) / 1024)
# Now that we know the file has been fully saved to disk move it into place.
os.remove(temp_file_path)
except OSError:
os.remove(temp_file_path)
request.session.flash('%s : Ce fichier existe déjà dans le rapport.' % input_name, 'danger')
@@ -420,6 +434,7 @@ def upload_img(request):
'norapport': norapport,
'rapport': rapport,
'photos': photos,
'total_size' : photos_size(photos),
'docs_url': request.static_url(request.registry.settings['mondumas.devfac_url']),
}
@@ -951,3 +966,23 @@ def demandes_generer(request, conn, mbx_name, mbx_search, liste, logged_in):
generer_dossier(request, mbx_name, mbx_search, extracted_file, temp_file_path, logged_in)
return
def resize_photos(image_file, new_image_file):
# using the Python Image Library (PIL) to resize an image
img_org = Image.open(image_file)
# get the size of the original image
width_org, height_org = img_org.size
# set the resizing factor so the aspect ratio can be retained
# factor > 1.0 increases size
# factor < 1.0 decreases size
factor = 0.75
width = int(width_org * factor)
height = int(height_org * factor)
# best down-sizing filter
img_anti = img_org.resize((width, height), Image.ANTIALIAS)
# split image filename into name and extension
name, ext = os.path.splitext(image_file)
# create a new file name for saving the result
img_anti.save(new_image_file)
return