Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,87 @@
|
|
1 |
-
import
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
|
|
|
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def enhance_image(image):
|
8 |
-
"""
|
9 |
-
if image is None:
|
10 |
-
# Return a simple test image if no input
|
11 |
-
arr = np.zeros((400, 400, 3), dtype=np.uint8)
|
12 |
-
arr.fill(128) # Gray image
|
13 |
-
return Image.fromarray(arr)
|
14 |
-
|
15 |
try:
|
16 |
-
# Convert
|
17 |
arr = np.array(image)
|
18 |
|
19 |
-
# Simple brightness
|
20 |
-
enhanced = np.clip(arr.astype(np.float32) * 1.
|
21 |
|
22 |
-
# Convert back to PIL
|
23 |
return Image.fromarray(enhanced)
|
24 |
-
|
25 |
except Exception as e:
|
26 |
-
print(f"
|
27 |
-
# Return original image if anything fails
|
28 |
return image
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
)
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
1 |
+
from flask import Flask, request, render_template_string, send_file
|
2 |
from PIL import Image
|
3 |
import numpy as np
|
4 |
+
import io
|
5 |
+
import base64
|
6 |
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
HTML_TEMPLATE = """
|
10 |
+
<!DOCTYPE html>
|
11 |
+
<html>
|
12 |
+
<head>
|
13 |
+
<title>ZeroIG Enhancement</title>
|
14 |
+
<style>
|
15 |
+
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
|
16 |
+
.container { text-align: center; }
|
17 |
+
.upload-area { border: 2px dashed #ccc; padding: 40px; margin: 20px 0; }
|
18 |
+
.result { margin-top: 20px; }
|
19 |
+
img { max-width: 100%; height: auto; }
|
20 |
+
</style>
|
21 |
+
</head>
|
22 |
+
<body>
|
23 |
+
<div class="container">
|
24 |
+
<h1>π ZeroIG: Low-Light Enhancement</h1>
|
25 |
+
<p>Upload a low-light image to enhance it!</p>
|
26 |
+
|
27 |
+
<form method="post" enctype="multipart/form-data">
|
28 |
+
<div class="upload-area">
|
29 |
+
<input type="file" name="image" accept="image/*" required>
|
30 |
+
<br><br>
|
31 |
+
<button type="submit">Enhance Image</button>
|
32 |
+
</div>
|
33 |
+
</form>
|
34 |
+
|
35 |
+
{% if result_image %}
|
36 |
+
<div class="result">
|
37 |
+
<h3>Enhanced Image:</h3>
|
38 |
+
<img src="data:image/png;base64,{{ result_image }}" alt="Enhanced">
|
39 |
+
<br><br>
|
40 |
+
<a href="data:image/png;base64,{{ result_image }}" download="enhanced.png">Download Enhanced Image</a>
|
41 |
+
</div>
|
42 |
+
{% endif %}
|
43 |
+
</div>
|
44 |
+
</body>
|
45 |
+
</html>
|
46 |
+
"""
|
47 |
|
48 |
def enhance_image(image):
|
49 |
+
"""Simple image enhancement"""
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
try:
|
51 |
+
# Convert to numpy array
|
52 |
arr = np.array(image)
|
53 |
|
54 |
+
# Simple brightness enhancement
|
55 |
+
enhanced = np.clip(arr.astype(np.float32) * 1.8, 0, 255).astype(np.uint8)
|
56 |
|
|
|
57 |
return Image.fromarray(enhanced)
|
|
|
58 |
except Exception as e:
|
59 |
+
print(f"Enhancement error: {e}")
|
|
|
60 |
return image
|
61 |
|
62 |
+
@app.route('/', methods=['GET', 'POST'])
|
63 |
+
def index():
|
64 |
+
result_image = None
|
65 |
+
|
66 |
+
if request.method == 'POST':
|
67 |
+
try:
|
68 |
+
file = request.files['image']
|
69 |
+
if file:
|
70 |
+
# Open and process image
|
71 |
+
image = Image.open(file.stream).convert('RGB')
|
72 |
+
enhanced = enhance_image(image)
|
73 |
+
|
74 |
+
# Convert to base64 for display
|
75 |
+
img_buffer = io.BytesIO()
|
76 |
+
enhanced.save(img_buffer, format='PNG')
|
77 |
+
img_str = base64.b64encode(img_buffer.getvalue()).decode()
|
78 |
+
result_image = img_str
|
79 |
+
|
80 |
+
except Exception as e:
|
81 |
+
print(f"Error processing image: {e}")
|
82 |
+
|
83 |
+
return render_template_string(HTML_TEMPLATE, result_image=result_image)
|
84 |
|
85 |
+
if __name__ == '__main__':
|
86 |
+
print("π Starting Flask app...")
|
87 |
+
app.run(host='0.0.0.0', port=7860)
|