-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
34 lines (23 loc) · 890 Bytes
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 7 03:17:34 2020
@author: kazzastic
"""
import os
from PIL import Image
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
def convertToJpg(app, filename):
image = Image.open(os.path.join(app.config['UPLOAD_FOLDER'], filename))
if '.' in filename and \
filename.rsplit('.', 1)[1].lower() in {'jpg', 'jpeg'}: # no need for conversion
return
new_file_name = filename.rsplit('.', 1)[0]+'.jpg'
image.save(os.path.join(app.config['UPLOAD_FOLDER'], new_file_name))
# deleting previous file
os.remove(os.path.join(app.config['UPLOAD_FOLDER'], filename))
# returning new file path
return os.path.join(app.config['UPLOAD_FOLDER'], new_file_name)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS