Spaces:
Runtime error
Runtime error
| from flask import Flask, request, render_template, jsonify | |
| import base64 | |
| import os | |
| app = Flask(__name__) | |
| # Folder where images will be saved | |
| UPLOAD_FOLDER = 'uploads' | |
| if not os.path.exists(UPLOAD_FOLDER): | |
| os.makedirs(UPLOAD_FOLDER) | |
| # Endpoint to handle the image | |
| def upload_image(): | |
| data = request.json['image'] | |
| # Remove the prefix from the base64 string (if present) | |
| if data.startswith('data:image/png;base64,'): | |
| data = data[len('data:image/png;base64,'):] | |
| # Decode the base64 string and save the image | |
| img_data = base64.b64decode(data) | |
| file_path = os.path.join(UPLOAD_FOLDER, 'captured_image.png') | |
| with open(file_path, 'wb') as f: | |
| f.write(img_data) | |
| return jsonify({"message": "Image saved successfully!", "path": file_path}) | |
| # Homepage | |
| def index(): | |
| return render_template('index.html') | |
| if __name__ == '__main__': | |
| app.run(debug=True) | |