otimizando flask

This commit is contained in:
José Henrique 2023-09-26 16:02:46 -03:00
parent 9082b34aab
commit 4ef0ab508f
2 changed files with 19 additions and 15 deletions

View File

@ -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

View File

@ -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