2023-08-31 18:15:50 +00:00
|
|
|
from wand.image import Image
|
|
|
|
|
2023-09-18 00:55:40 +00:00
|
|
|
from static.image_helper import ImageHelper
|
|
|
|
|
2023-08-31 18:15:50 +00:00
|
|
|
|
|
|
|
def box_blur_image_separable(image, blurred_image, radius_x, radius_y):
|
|
|
|
pixels = image.get_pixels()
|
|
|
|
blurred_pixels = blurred_image.get_pixels()
|
|
|
|
|
|
|
|
for pixel in pixels:
|
|
|
|
x, y = pixel[0], pixel[1]
|
|
|
|
|
|
|
|
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 < image.width and 0 <= new_y < image.height:
|
|
|
|
pixel_color = pixels[new_y * image.width + new_x]
|
|
|
|
r_total += pixel_color.red
|
|
|
|
g_total += pixel_color.green
|
|
|
|
b_total += pixel_color.blue
|
|
|
|
pixel_count += 1
|
|
|
|
|
|
|
|
blurred_pixel = blurred_pixels[y * image.width + x]
|
|
|
|
blurred_pixel.red = int(r_total / pixel_count)
|
|
|
|
blurred_pixel.green = int(g_total / pixel_count)
|
|
|
|
blurred_pixel.blue = int(b_total / pixel_count)
|
|
|
|
|
|
|
|
return blurred_image
|
|
|
|
|
|
|
|
|
2023-09-18 00:55:40 +00:00
|
|
|
def save_image(file_stream):
|
|
|
|
with open("image.png", "wb") as file:
|
|
|
|
file.write(file_stream.read())
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
|
2023-08-31 18:15:50 +00:00
|
|
|
class ImageService:
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
2023-09-18 00:55:40 +00:00
|
|
|
def box_blur_image(self, img, radius):
|
2023-08-31 18:15:50 +00:00
|
|
|
temp_path = 'temp_image.png'
|
2023-09-18 00:55:40 +00:00
|
|
|
img.save(temp_path)
|
2023-08-31 18:15:50 +00:00
|
|
|
|
|
|
|
with Image(filename=temp_path) as img:
|
2023-09-18 00:55:40 +00:00
|
|
|
img.blur(radius, 2)
|
2023-08-31 18:15:50 +00:00
|
|
|
blurred_temp_path = 'blurred_temp_image.png'
|
2023-09-18 00:55:40 +00:00
|
|
|
img.save(filename='blurred_temp_image.png')
|
2023-08-31 18:15:50 +00:00
|
|
|
return blurred_temp_path
|
|
|
|
|
|
|
|
|
|
|
|
def get_simple_image(self):
|
2023-09-18 00:55:40 +00:00
|
|
|
with ImageHelper.SimpleImage as img:
|
|
|
|
img = ImageHelper.SimpleImage
|
|
|
|
simple_image = 'simple_image.png'
|
|
|
|
img.save(filename='simple_image.png')
|
|
|
|
return simple_image
|
|
|
|
|
2023-08-31 18:15:50 +00:00
|
|
|
|
|
|
|
def get_big_image(self):
|
2023-09-18 00:55:40 +00:00
|
|
|
with ImageHelper.BigImage as img:
|
|
|
|
img = ImageHelper.BigImage
|
|
|
|
big_image = 'big_image.png'
|
|
|
|
img.save(filename='big_image.png')
|
|
|
|
return big_image
|