From 4ef0ab508fe9aadc2e78b44305e86ca53e0ec092 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Henrique?= Date: Tue, 26 Sep 2023 16:02:46 -0300 Subject: [PATCH] otimizando flask --- FlaskAPI/controllers/image.py | 6 +++--- FlaskAPI/services/image.py | 28 ++++++++++++++++------------ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/FlaskAPI/controllers/image.py b/FlaskAPI/controllers/image.py index aac4dbc..6fb10b9 100644 --- a/FlaskAPI/controllers/image.py +++ b/FlaskAPI/controllers/image.py @@ -11,10 +11,10 @@ def blur_image(): image = request.get_data() if radius and image: - return send_file(image_service.box_blur_image(image, radius), - mimetype='image/jpeg', + return send_file(io.BytesIO(image_service.box_blur_image(image, radius)), + mimetype='image/png', as_attachment=True, - download_name='blurred_image.jpeg') + download_name='blurred_image.png') return "Bad request", 400 diff --git a/FlaskAPI/services/image.py b/FlaskAPI/services/image.py index e4caeac..383f16b 100644 --- a/FlaskAPI/services/image.py +++ b/FlaskAPI/services/image.py @@ -1,29 +1,33 @@ -from wand.image import Image +from wand.image import Image, Color def box_blur_image_separable(image, radius_x, radius_y): - blurred_image = image.clone() width, height = image.width, image.height + kernel_x_size = 2 * radius_x + 1 + kernel_y_size = 2 * radius_y + 1 + kernel_area = kernel_x_size * kernel_y_size + + blurred_image = image.clone() for y in range(height): for x in range(width): - blurred_image[x, y] = image[x, y] - r_total, g_total, b_total = 0, 0, 0 - pixel_count = 0 + for offset_y in range(-radius_y, radius_y + 1): for offset_x in range(-radius_x, radius_x + 1): new_x = x + offset_x new_y = y + offset_y if 0 <= new_x < width and 0 <= new_y < height: - r_total += image[new_x, new_y].red_int8 - g_total += image[new_x, new_y].green_int8 - b_total += image[new_x, new_y].blue_int8 - pixel_count += 1 + pixel = image[new_x, new_y] + r_total += pixel.red_int8 + g_total += pixel.green_int8 + b_total += pixel.blue_int8 - blurred_image[x, y].red_int8 = int(r_total / pixel_count) - blurred_image[x, y].green_int8 = int(g_total / pixel_count) - blurred_image[x, y].blue_int8 = int(b_total / pixel_count) + r_avg = int(r_total / kernel_area) + g_avg = int(g_total / kernel_area) + b_avg = int(b_total / kernel_area) + + blurred_image[x, y] = Color(f'rgb({r_avg},{g_avg},{b_avg})') return blurred_image