Created
November 14, 2021 09:08
-
-
Save himaprasoonpt/01c459eb5d4c9311ffa54e045133080a to your computer and use it in GitHub Desktop.
convert_to_300kb
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
import os | |
from flask import Flask, flash, request, redirect, url_for | |
from werkzeug.utils import secure_filename | |
import tempfile | |
tempdir = tempfile.mkdtemp(prefix="temp_image_upload") | |
UPLOAD_FOLDER = tempdir | |
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'} | |
print(UPLOAD_FOLDER) | |
app = Flask(__name__) | |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
def allowed_file(filename): | |
return '.' in filename and \ | |
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS | |
from flask import send_from_directory, send_file | |
from PIL import Image | |
import os | |
import math | |
def resize_image_to_specific_size(input_path): | |
required_image_size_in_kb = 50 | |
img = Image.open(input_path) | |
print(img.size) | |
kb_size = os.stat(path=input_path).st_size / 1000 | |
print(kb_size) | |
resize_factor = required_image_size_in_kb / kb_size | |
print(resize_factor) | |
img = img.resize((math.floor(img.size[0] * resize_factor), math.floor(img.size[1] * resize_factor))) | |
if img.mode in ("RGBA", "P"): | |
img = img.convert("RGB") | |
img.save(input_path) | |
@app.route('/uploads/<filename>') | |
def uploaded_file(filename): | |
return send_from_directory(app.config['UPLOAD_FOLDER'], | |
filename) | |
invlaid_file_html = ''' | |
<!doctype html> | |
<title>Invalid file</title> | |
<h1>Invalid file</h1> | |
<a href="/">Click here to Try again</a> | |
''' | |
@app.route('/', methods=['GET', 'POST']) | |
def upload_file(): | |
if request.method == 'POST': | |
# check if the post request has the file part | |
if 'file' not in request.files: | |
flash('No file part') | |
return redirect(request.url) | |
file = request.files['file'] | |
# if user does not select file, browser also | |
# submit an empty part without filename | |
if file.filename == '': | |
flash('No selected file') | |
return redirect(request.url) | |
if file and allowed_file(file.filename): | |
try: | |
filename = secure_filename(file.filename) | |
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | |
file.save(file_path) | |
resize_image_to_specific_size(file_path) | |
return send_file(file_path, as_attachment=True) | |
except : | |
return invlaid_file_html | |
else: | |
return invlaid_file_html | |
return ''' | |
<!doctype html> | |
<title>Upload new File</title> | |
<h1>Upload new File</h1> | |
<form method=post enctype=multipart/form-data> | |
<input type=file name=file> | |
<input type=submit value=Upload> | |
</form> | |
''' | |
if __name__ == '__main__': | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment