File size: 1,274 Bytes
ecaf87a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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')