tcc/FlaskAPI/services/image.py

54 lines
1.8 KiB
Python
Raw Normal View History

from wand.image import Image
2023-09-24 13:56:01 +00:00
def box_blur_image_separable(image, radius_x, radius_y):
blurred_image = image.clone()
width, height = image.width, image.height
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
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)
return blurred_image
class ImageService:
def __init__(self):
pass
def box_blur_image(self, img, radius):
2023-09-21 20:57:47 +00:00
with Image(blob=img) as image:
2023-09-24 13:56:01 +00:00
blurred_image = box_blur_image_separable(image, radius, 0)
blurred_image = box_blur_image_separable(blurred_image, 0, radius)
return blurred_image.make_blob()
def get_simple_image(self):
2023-09-18 15:24:05 +00:00
with open("./static/small-image.png", "rb") as file:
return file.read()
def get_big_image(self):
2023-09-18 15:24:05 +00:00
with open("./static/big-image.png", "rb") as file:
return file.read()
def save_image(self, img):
2023-09-21 20:57:47 +00:00
with open("image.png", "wb") as file:
file.write(img)
2023-09-24 13:56:01 +00:00
return True