Spaces:
Paused
Paused
giorgio-caparvi
commited on
Commit
·
fa22b90
1
Parent(s):
da27942
added post test for image
Browse files- api/app.py +34 -0
api/app.py
CHANGED
|
@@ -114,5 +114,39 @@ def test_post():
|
|
| 114 |
# Restituisci una risposta con i dati ricevuti
|
| 115 |
return jsonify({'message': 'POST funzionante', 'data_received': data}), 200
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
if __name__ == '__main__':
|
| 118 |
app.run(host='0.0.0.0', port=7860, debug=True)
|
|
|
|
| 114 |
# Restituisci una risposta con i dati ricevuti
|
| 115 |
return jsonify({'message': 'POST funzionante', 'data_received': data}), 200
|
| 116 |
|
| 117 |
+
@app.route('/process-image', methods=['POST'])
|
| 118 |
+
def process_image():
|
| 119 |
+
try:
|
| 120 |
+
# Ottieni il JSON dalla richiesta
|
| 121 |
+
json_data = request.get_json()
|
| 122 |
+
if not json_data or 'image' not in json_data:
|
| 123 |
+
return "Invalid or missing JSON data", 400
|
| 124 |
+
|
| 125 |
+
# Decodifica l'immagine base64
|
| 126 |
+
encoded_image = json_data['image']
|
| 127 |
+
try:
|
| 128 |
+
image_data = base64.b64decode(encoded_image)
|
| 129 |
+
image = Image.open(io.BytesIO(image_data))
|
| 130 |
+
print("### Immagine aperta con successo ###") # Stampa per il debug
|
| 131 |
+
except Exception as e:
|
| 132 |
+
print(f"### Errore nell'apertura dell'immagine: {str(e)} ###") # Stampa per il debug
|
| 133 |
+
return f"Failed to open the image: {str(e)}", 400
|
| 134 |
+
|
| 135 |
+
# Elaborazione dell'immagine con PIL (ad esempio, convertiamo l'immagine in scala di grigi)
|
| 136 |
+
processed_image = image.convert("L") # Converti l'immagine in scala di grigi
|
| 137 |
+
|
| 138 |
+
# Salva l'immagine su un buffer
|
| 139 |
+
img_io = io.BytesIO()
|
| 140 |
+
processed_image.save(img_io, 'JPEG')
|
| 141 |
+
img_io.seek(0)
|
| 142 |
+
|
| 143 |
+
# Restituisci l'immagine come file scaricabile
|
| 144 |
+
return send_file(img_io, mimetype='image/jpeg', as_attachment=True, download_name='processed_image.jpg')
|
| 145 |
+
|
| 146 |
+
except Exception as e:
|
| 147 |
+
print(f"### Errore durante l'elaborazione dell'immagine: {str(e)} ###") # Stampa per il debug
|
| 148 |
+
return str(e), 500
|
| 149 |
+
|
| 150 |
+
|
| 151 |
if __name__ == '__main__':
|
| 152 |
app.run(host='0.0.0.0', port=7860, debug=True)
|