-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
170 lines (151 loc) · 4.75 KB
/
Copy pathapp.py
File metadata and controls
170 lines (151 loc) · 4.75 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
from flask import Flask, request, send_file, render_template_string, redirect, url_for
from PIL import Image
import os
from io import BytesIO
from datetime import datetime
app = Flask(__name__)
UPLOAD_FOLDER = "uploaded_images"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>LWS25 - Image Compressor & Converter</title>
<style>
body {
font-family: 'Segoe UI', sans-serif;
background: #101010;
color: #e0e0e0;
padding: 20px;
max-width: 800px;
margin: auto;
}
h1 {
text-align: center;
font-size: 2rem;
margin-bottom: 1rem;
}
form {
background: #1a1a1a;
padding: 20px;
border-radius: 16px;
box-shadow: 0 0 10px #000;
margin-bottom: 20px;
}
label {
display: block;
margin-top: 10px;
}
input, select {
margin-top: 5px;
padding: 10px;
width: 100%;
border-radius: 8px;
border: none;
background: #2a2a2a;
color: white;
}
button {
margin-top: 20px;
padding: 10px 20px;
background: #00cc99;
border: none;
border-radius: 12px;
cursor: pointer;
transition: 0.3s;
}
button:hover {
background: #00b386;
}
.image-preview {
text-align: center;
margin-top: 20px;
}
.info {
background: #222;
padding: 15px;
border-radius: 12px;
margin-top: 10px;
}
a {
color: #00ccff;
text-decoration: none;
}
</style>
</head>
<body>
<h1>LWS25 - Image Compressor & Converter</h1>
<form method="POST" enctype="multipart/form-data">
<label>Upload Image:</label>
<input type="file" name="image" accept="image/*" required>
<label>Select Quality (for compression):</label>
<select name="quality">
<option value="100">100% (Original)</option>
<option value="80">80%</option>
<option value="60">60%</option>
<option value="40">40%</option>
</select>
<label>Convert to Format:</label>
<select name="format">
<option value="original">Keep Original</option>
<option value="JPEG">JPEG</option>
<option value="PNG">PNG</option>
<option value="WEBP">WEBP</option>
</select>
<button type="submit">Process Image</button>
</form>
{% if filename %}
<div class="image-preview">
<h2>Result</h2>
<img src="{{ url_for('get_image', filename=filename) }}" style="max-width: 100%; border-radius: 12px;"><br><br>
<a href="{{ url_for('download_image', filename=filename) }}">Download Image</a><br>
<form method="POST" action="{{ url_for('delete_image', filename=filename) }}">
<button type="submit" style="background:#cc0000;margin-top:10px;">Delete File</button>
</form>
<div class="info">
<strong>Size:</strong> {{ size }} KB<br>
<strong>Format:</strong> {{ filetype }}<br>
<strong>Created:</strong> {{ created }}
</div>
</div>
{% endif %}
</body>
</html>
"""
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
file = request.files["image"]
quality = int(request.form["quality"])
convert_format = request.form["format"]
if not file:
return redirect(url_for("index"))
img = Image.open(file.stream)
original_format = img.format
ext = convert_format.lower() if convert_format != "original" else original_format.lower()
filename = f"{datetime.now().strftime('%Y%m%d%H%M%S')}.{ext}"
filepath = os.path.join(UPLOAD_FOLDER, filename)
save_format = convert_format if convert_format != "original" else original_format
# Convert mode for JPEG
if save_format == "JPEG" and img.mode in ("RGBA", "P"):
img = img.convert("RGB")
img.save(filepath, format=save_format, quality=quality)
size_kb = round(os.path.getsize(filepath) / 1024, 2)
created_time = datetime.fromtimestamp(os.path.getctime(filepath)).strftime("%Y-%m-%d %H:%M:%S")
return render_template_string(TEMPLATE, filename=filename, size=size_kb, filetype=save_format, created=created_time)
return render_template_string(TEMPLATE, filename=None)
@app.route("/image/<filename>")
def get_image(filename):
return send_file(os.path.join(UPLOAD_FOLDER, filename))
@app.route("/download/<filename>")
def download_image(filename):
return send_file(os.path.join(UPLOAD_FOLDER, filename), as_attachment=True)
@app.route("/delete/<filename>", methods=["POST"])
def delete_image(filename):
filepath = os.path.join(UPLOAD_FOLDER, filename)
if os.path.exists(filepath):
os.remove(filepath)
return redirect(url_for("index"))
if __name__ == "__main__":
app.run(debug=False)