nagasurendra commited on
Commit
ecaf87a
·
verified ·
1 Parent(s): e515203

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, render_template, jsonify
2
+ import os
3
+ import base64
4
+ from PIL import Image
5
+ import io
6
+ import uuid
7
+
8
+ app = Flask(__name__)
9
+
10
+ # Define the upload folder
11
+ UPLOAD_FOLDER = 'uploads'
12
+ if not os.path.exists(UPLOAD_FOLDER):
13
+ os.makedirs(UPLOAD_FOLDER)
14
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
15
+
16
+ @app.route('/')
17
+ def index():
18
+ return render_template('index.html')
19
+
20
+ @app.route('/capture', methods=['POST'])
21
+ def capture():
22
+ try:
23
+ # Get the base64 image data from the request
24
+ data = request.form['image']
25
+ # Remove the base64 prefix (e.g., "data:image/png;base64,")
26
+ image_data = data.split(',')[1]
27
+ # Decode the base64 string
28
+ image_bytes = base64.b64decode(image_data)
29
+ # Create an image from the bytes
30
+ image = Image.open(io.BytesIO(image_bytes))
31
+ # Generate a unique filename
32
+ filename = f"image_{uuid.uuid4()}.png"
33
+ filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
34
+ # Save the image
35
+ image.save(filepath, 'PNG')
36
+ return jsonify({'message': 'Image saved successfully', 'filename': filename})
37
+ except Exception as e:
38
+ return jsonify({'error': str(e)}), 500
39
+
40
+ if __name__ == '__main__':
41
+ app.run(debug=True, host='0.0.0.0')