File size: 1,242 Bytes
def2fb4 f57b188 def2fb4 f57b188 def2fb4 f57b188 def2fb4 e349159 def2fb4 e247e6b def2fb4 f57b188 def2fb4 f57b188 e247e6b def2fb4 e349159 f57b188 def2fb4 f57b188 def2fb4 f57b188 def2fb4 |
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 |
from flask import Flask, jsonify, request, send_file
from flask_cors import CORS
from PIL import Image
import io
# Initialize the Flask app
myapp = Flask(__name__)
CORS(myapp) # Enable CORS if needed
@myapp.route('/')
def home():
return "Welcome to the Image Upscaler!" # Basic home response
@myapp.route('/upscale', methods=['POST'])
def upscale_image():
if 'image' not in request.files:
return jsonify({"error": "No image provided"}), 400
input_image = request.files['image'].read() # Read the uploaded image
width = int(request.form.get('width', 2)) # User-specified upscale factor
height = int(request.form.get('height', 2))
# Open the image using PIL
img = Image.open(io.BytesIO(input_image))
# Resize the image using user-defined width and height
upscaled_img = img.resize((img.width * width, img.height * height), Image.LANCZOS)
# Save to bytes
img_byte_arr = io.BytesIO()
upscaled_img.save(img_byte_arr, format='PNG')
img_byte_arr.seek(0)
return send_file(img_byte_arr, mimetype='image/png')
# Add this block to make sure your app runs when called
if __name__ == "__main__":
myapp.run(host='0.0.0.0', port=7860) # Run directly if needed for testing |