Geek7 commited on
Commit
def2fb4
·
verified ·
1 Parent(s): f8d1b83

Update myapp.py

Browse files
Files changed (1) hide show
  1. myapp.py +26 -19
myapp.py CHANGED
@@ -1,31 +1,38 @@
1
- from flask import Flask, request, jsonify
2
  from flask_cors import CORS
3
  from PIL import Image
 
4
 
5
  # Initialize the Flask app
6
  myapp = Flask(__name__)
7
-
8
- # Enable CORS for the app
9
- CORS(myapp)
10
 
11
  @myapp.route('/')
12
- def index():
13
- return "Welcome to the AI Model Upscale!"
 
 
 
 
 
 
 
 
 
14
 
15
- @myapp.route('/resize', methods=['POST'])
16
- def resize_image():
17
- data = request.json
18
- input_path = data.get('input_path')
19
- output_path = data.get('output_path')
20
- width = data.get('width')
21
- height = data.get('height')
22
 
23
- # Open an image file and resize it
24
- with Image.open(input_path) as img:
25
- resized_img = img.resize((width, height), Image.LANCZOS)
26
- resized_img.save(output_path)
 
 
 
27
 
28
- return jsonify({'message': 'Image resized successfully', 'output_path': output_path})
29
 
 
30
  if __name__ == "__main__":
31
- myapp.run(host='0.0.0.0', port=5000)
 
1
+ from flask import Flask, jsonify, request, send_file
2
  from flask_cors import CORS
3
  from PIL import Image
4
+ import io
5
 
6
  # Initialize the Flask app
7
  myapp = Flask(__name__)
8
+ CORS(myapp) # Enable CORS if needed
 
 
9
 
10
  @myapp.route('/')
11
+ def home():
12
+ return "Welcome to the Image Upscaler!" # Basic home response
13
+
14
+ @myapp.route('/upscale', methods=['POST'])
15
+ def upscale_image():
16
+ if 'image' not in request.files:
17
+ return jsonify({"error": "No image provided"}), 400
18
+
19
+ input_image = request.files['image'].read() # Read the uploaded image
20
+ width = int(request.form.get('width', 2)) # Default upscale factor
21
+ height = int(request.form.get('height', 2))
22
 
23
+ # Open the image using PIL
24
+ img = Image.open(io.BytesIO(input_image))
 
 
 
 
 
25
 
26
+ # Resize the image to upscale (you can adjust the method as needed)
27
+ upscaled_img = img.resize((img.width * width, img.height * height), Image.LANCZOS)
28
+
29
+ # Save to bytes
30
+ img_byte_arr = io.BytesIO()
31
+ upscaled_img.save(img_byte_arr, format='PNG')
32
+ img_byte_arr.seek(0)
33
 
34
+ return send_file(img_byte_arr, mimetype='image/png')
35
 
36
+ # Add this block to make sure your app runs when called
37
  if __name__ == "__main__":
38
+ myapp.run(host='0.0.0.0', port=7860) # Run directly if needed for testing