-
Notifications
You must be signed in to change notification settings - Fork 0
/
working 2.py
100 lines (83 loc) · 4.2 KB
/
working 2.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from flask import Flask, render_template, request, redirect, url_for
from werkzeug.utils import secure_filename
from PIL import Image
from flask import send_file
import os
app = Flask(__name__, template_folder='D:/Image-resizer-dont-delete-akhil/Image-Size-Compressor-main/templates', static_folder='D:/Image-resizer-dont-delete-akhil/Image-Size-Compressor-main/static')
# Set the base directory
BASE_DIR = 'D:/Image-resizer-dont-delete-akhil/Image-Size-Compressor-main'
# Set the upload folder and allowed extensions
app.config['UPLOAD_FOLDER'] = os.path.join(BASE_DIR, 'uploads')
app.config['COMPRESSED_FOLDER'] = os.path.join(BASE_DIR, 'static/compressed')
app.config['ALLOWED_EXTENSIONS'] = {'jpg', 'jpeg', 'png', 'gif'}
def allowed_file(filename):
# Check if the file extension is allowed
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
def compress_image(input_path, output_path, desired_size, desired_byte_format, photo_option):
# Open the input image
with Image.open(input_path) as img:
# Calculate the initial size of the image
size = os.path.getsize(input_path)
# Convert the desired size to bytes
if desired_byte_format == 'KB':
desired_size = int(desired_size.replace("KB", "")) * 1024
elif desired_byte_format == 'MB':
desired_size = int(desired_size.replace("MB", "")) * 1024 * 1024
elif desired_byte_format == 'GB':
desired_size = int(desired_size.replace("GB", "")) * 1024 * 1024 * 1024
else:
raise ValueError("Invalid size format! Use KB, MB, or GB.")
# If the image size is already smaller than the desired size, return the input image
if size < desired_size:
return img
# Apply photo option if selected
if photo_option:
width, height = img.size
img = img.resize((200, 250))
# Initialize the quality parameter
quality = 100
# Compress the image until the desired size is reached
while size > desired_size:
if quality <= 0:
break
img.convert("RGB").save(output_path, "JPEG", quality=quality)
size = os.path.getsize(output_path)
quality -= 5
# Return the compressed image
return img
@app.route('/', methods=['GET', 'POST'])
def index():
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)
print("1 clear")
file = request.files['file']
# If the user does not select a file, submit an empty part without filename
if file.filename == '':
flash('No selected file')
return redirect(request.url)
# Check if the file is allowed
if file and allowed_file(file.filename):
# Save the uploaded file to the uploads folder
filename = secure_filename(file.filename)
input_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(input_path)
print("2")
# Compress the image and save it to the uploads folder with a "_compressed" suffix
output_path = os.path.join(app.config['COMPRESSED_FOLDER'], os.path.splitext(filename)[0] + "_compressed.jpg")
desired_size = request.form['size']
desired_byte_format = request.form['option']
photo_option = 'photo-option' in request.form # Check if photo option is selected
compress_image(input_path, output_path, desired_size, desired_byte_format, photo_option)
# Return the download link for the compressed image
return render_template('result.html', filename=os.path.basename(output_path))
return render_template('index.html')
@app.route('/static/compressed/<filename>')
def download(filename):
# Return the download link for the compressed image
path = os.path.join(app.config['COMPRESSED_FOLDER'], filename)
return send_file(path, as_attachment=True)
if __name__ == '__main__':
app.run(debug=True)