File size: 968 Bytes
17d84f4
 
 
 
 
 
9a9d5c5
 
 
 
 
17d84f4
 
 
 
 
 
 
 
 
 
 
9a9d5c5
 
 
17d84f4
 
9a9d5c5
17d84f4
 
 
 
 
 
 
 
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
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
@app.route('/upload', methods=['POST'])
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
@app.route('/')
def index():
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)