from flask import Flask, request, render_template, jsonify import os import base64 from PIL import Image import io import uuid app = Flask(__name__) # Define the upload folder UPLOAD_FOLDER = 'uploads' if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER @app.route('/') def index(): return render_template('index.html') @app.route('/capture', methods=['POST']) def capture(): try: # Get the base64 image data from the request data = request.form['image'] # Remove the base64 prefix (e.g., "data:image/png;base64,") image_data = data.split(',')[1] # Decode the base64 string image_bytes = base64.b64decode(image_data) # Create an image from the bytes image = Image.open(io.BytesIO(image_bytes)) # Generate a unique filename filename = f"image_{uuid.uuid4()}.png" filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) # Save the image image.save(filepath, 'PNG') return jsonify({'message': 'Image saved successfully', 'filename': filename}) except Exception as e: return jsonify({'error': str(e)}), 500 if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')