Update myapp.py
Browse files
myapp.py
CHANGED
@@ -1,6 +1,8 @@
|
|
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
|
@@ -9,7 +11,7 @@ CORS(myapp) # Enable CORS if needed
|
|
9 |
|
10 |
@myapp.route('/')
|
11 |
def home():
|
12 |
-
return "Welcome to the Image Upscaler
|
13 |
|
14 |
@myapp.route('/upscale', methods=['POST'])
|
15 |
def upscale_image():
|
@@ -23,13 +25,18 @@ def upscale_image():
|
|
23 |
# Open the image using PIL
|
24 |
img = Image.open(io.BytesIO(input_image))
|
25 |
|
26 |
-
# Resize the image to upscale
|
27 |
upscaled_img = img.resize((img.width * width, img.height * height), Image.LANCZOS)
|
28 |
-
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
return send_file(img_byte_arr, mimetype='image/png')
|
35 |
|
|
|
1 |
from flask import Flask, jsonify, request, send_file
|
2 |
from flask_cors import CORS
|
3 |
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
import cv2
|
6 |
import io
|
7 |
|
8 |
# Initialize the Flask app
|
|
|
11 |
|
12 |
@myapp.route('/')
|
13 |
def home():
|
14 |
+
return "Welcome to the Image Upscaler and Noise Reducer!"
|
15 |
|
16 |
@myapp.route('/upscale', methods=['POST'])
|
17 |
def upscale_image():
|
|
|
25 |
# Open the image using PIL
|
26 |
img = Image.open(io.BytesIO(input_image))
|
27 |
|
28 |
+
# Resize the image to upscale
|
29 |
upscaled_img = img.resize((img.width * width, img.height * height), Image.LANCZOS)
|
30 |
+
|
31 |
+
# Convert PIL image to numpy array for OpenCV processing
|
32 |
+
np_img = np.array(upscaled_img)
|
33 |
+
|
34 |
+
# Apply median filtering for noise reduction
|
35 |
+
filtered_img = cv2.medianBlur(np_img, 5) # The second parameter is the kernel size
|
36 |
+
|
37 |
+
# Convert the result back to PIL Image for sending
|
38 |
+
_, buffer = cv2.imencode('.png', filtered_img)
|
39 |
+
img_byte_arr = io.BytesIO(buffer)
|
40 |
|
41 |
return send_file(img_byte_arr, mimetype='image/png')
|
42 |
|