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