|
from flask import Flask, jsonify, request, send_file |
|
from flask_cors import CORS |
|
from PIL import Image |
|
import io |
|
|
|
|
|
myapp = Flask(__name__) |
|
CORS(myapp) |
|
|
|
@myapp.route('/') |
|
def home(): |
|
return "Welcome to the Image Upscaler!" |
|
|
|
@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() |
|
width = int(request.form.get('width', 2)) |
|
height = int(request.form.get('height', 2)) |
|
|
|
|
|
img = Image.open(io.BytesIO(input_image)) |
|
|
|
|
|
upscaled_img = img.resize((img.width * width, img.height * height), Image.LANCZOS) |
|
|
|
|
|
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') |
|
|
|
|
|
if __name__ == "__main__": |
|
myapp.run(host='0.0.0.0', port=7860) |