Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
import os
|
4 |
from flask import Flask, request, send_file, jsonify
|
5 |
from rembg import remove
|
6 |
-
from PIL import Image
|
7 |
import io
|
8 |
|
9 |
# --- Create the Flask App ---
|
@@ -113,6 +113,66 @@ def compress_image_api():
|
|
113 |
|
114 |
return jsonify({"error": "An unknown error occurred"}), 500
|
115 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
# --- Run the App ---
|
117 |
if __name__ == '__main__':
|
118 |
# For local testing, ensure the environment variable is set.
|
|
|
3 |
import os
|
4 |
from flask import Flask, request, send_file, jsonify
|
5 |
from rembg import remove
|
6 |
+
from PIL import Image, ImageEnhance, ImageFilter
|
7 |
import io
|
8 |
|
9 |
# --- Create the Flask App ---
|
|
|
113 |
|
114 |
return jsonify({"error": "An unknown error occurred"}), 500
|
115 |
|
116 |
+
# --- AI IMAGE ENHANCER API ENDPOINT ---
|
117 |
+
|
118 |
+
@app.route('/enhance-image', methods=['POST'])
|
119 |
+
def enhance_image_api():
|
120 |
+
# 1. --- API Key Authentication (Bilkul pehle jaisa) ---
|
121 |
+
api_key_header = request.headers.get('x-api-key')
|
122 |
+
if not api_key_header or api_key_header != API_KEY:
|
123 |
+
return jsonify({"error": "Unauthorized. Invalid or missing API Key."}), 401
|
124 |
+
|
125 |
+
# 2. --- File Check Karna ---
|
126 |
+
if 'file' not in request.files:
|
127 |
+
return jsonify({"error": "No file part in the request"}), 400
|
128 |
+
|
129 |
+
file = request.files['file']
|
130 |
+
|
131 |
+
if file.filename == '':
|
132 |
+
return jsonify({"error": "No selected file"}), 400
|
133 |
+
|
134 |
+
# 3. --- Image Processing (Pillow Library ka Jadoo) ---
|
135 |
+
if file:
|
136 |
+
try:
|
137 |
+
# File ko memory mein bytes ki tarah parhna
|
138 |
+
input_image_bytes = file.read()
|
139 |
+
|
140 |
+
# Pillow ka istemal karke image ko kholna
|
141 |
+
img = Image.open(io.BytesIO(input_image_bytes))
|
142 |
+
|
143 |
+
# Enhancement Step 1: Contrast ko behtar karna
|
144 |
+
# 1.0 original contrast hai. 1.2 ka matlab hai 20% zyada contrast.
|
145 |
+
enhancer_contrast = ImageEnhance.Contrast(img)
|
146 |
+
img = enhancer_contrast.enhance(1.2)
|
147 |
+
|
148 |
+
# Enhancement Step 2: Sharpness ko behtar karna
|
149 |
+
# 1.0 original sharpness hai. 1.4 ka matlab hai 40% zyada sharp.
|
150 |
+
enhancer_sharpness = ImageEnhance.Sharpness(img)
|
151 |
+
img = enhancer_sharpness.enhance(1.4)
|
152 |
+
|
153 |
+
# Ek khali "in-memory" file banana
|
154 |
+
output_buffer = io.BytesIO()
|
155 |
+
|
156 |
+
# Enhanced image ko buffer mein save karna
|
157 |
+
# PNG format mein save karein taake transparency (agar ho) barqarar rahe
|
158 |
+
img.save(output_buffer, format='PNG')
|
159 |
+
|
160 |
+
# Enhanced image ke bytes haasil karna
|
161 |
+
output_image_bytes = output_buffer.getvalue()
|
162 |
+
|
163 |
+
# 4. --- Send the Response ---
|
164 |
+
# Enhanced image ko user ko wapas bhejna
|
165 |
+
return send_file(
|
166 |
+
io.BytesIO(output_image_bytes),
|
167 |
+
mimetype='image/png',
|
168 |
+
as_attachment=True,
|
169 |
+
download_name='enhanced_image.png'
|
170 |
+
)
|
171 |
+
except Exception as e:
|
172 |
+
return jsonify({"error": "Failed to enhance image", "details": str(e)}), 500
|
173 |
+
|
174 |
+
return jsonify({"error": "An unknown error occurred"}), 500
|
175 |
+
|
176 |
# --- Run the App ---
|
177 |
if __name__ == '__main__':
|
178 |
# For local testing, ensure the environment variable is set.
|