From f0fa227035cb153d1f116056db3747ac9d39cf6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Henrique?= Date: Sun, 24 Sep 2023 10:56:01 -0300 Subject: [PATCH] ajustando ASP com Flask --- ASP.NET/Controllers/ImageController.cs | 2 + ASP.NET/ImageHelper.cs | 16 -------- ASP.NET/Services/ImageService.cs | 5 +-- ActixAPI/Dockerfile | 50 ++++++++++++------------- FlaskAPI/Dockerfile | 2 +- FlaskAPI/app.py | 8 ++-- FlaskAPI/controllers/image.py | 9 +++-- FlaskAPI/services/image.py | 51 +++++++++++++------------- FlaskAPI/static/image_helper.py | 14 ------- docker-compose.yml | 16 ++++++++ 10 files changed, 80 insertions(+), 93 deletions(-) delete mode 100644 ASP.NET/ImageHelper.cs delete mode 100644 FlaskAPI/static/image_helper.py diff --git a/ASP.NET/Controllers/ImageController.cs b/ASP.NET/Controllers/ImageController.cs index 8c57ecf..f0a4ed5 100644 --- a/ASP.NET/Controllers/ImageController.cs +++ b/ASP.NET/Controllers/ImageController.cs @@ -50,6 +50,8 @@ namespace TCC.Controllers MemoryStream mstream = new MemoryStream(); await HttpContext.Request.Body.CopyToAsync(mstream); mstream.Position = 0; + + ImageService.SaveImage(mstream); mstream.Close(); return Ok(); diff --git a/ASP.NET/ImageHelper.cs b/ASP.NET/ImageHelper.cs deleted file mode 100644 index df0b189..0000000 --- a/ASP.NET/ImageHelper.cs +++ /dev/null @@ -1,16 +0,0 @@ -using ImageMagick; - -namespace tcc_app -{ - public static class ImageHelper - { - public static byte[] SimpleImage; - public static byte[] BigImage; - - static ImageHelper() - { - SimpleImage = File.ReadAllBytes("static/small-image.png"); - BigImage = File.ReadAllBytes("static/big-image.png"); - } - } -} diff --git a/ASP.NET/Services/ImageService.cs b/ASP.NET/Services/ImageService.cs index 99e2861..dfa6ce3 100644 --- a/ASP.NET/Services/ImageService.cs +++ b/ASP.NET/Services/ImageService.cs @@ -1,5 +1,4 @@ using ImageMagick; -using tcc_app; namespace TCC.Services { @@ -13,7 +12,7 @@ namespace TCC.Services var blurredImage = new MagickImage(image); blurredImage = BoxBlurImageSeparable(image, blurredImage, radius, 0); - blurredImage = BoxBlurImageSeparable(image, blurredImage, 0, radius); + blurredImage = BoxBlurImageSeparable(blurredImage, blurredImage, 0, radius); return blurredImage; } @@ -65,13 +64,11 @@ namespace TCC.Services public byte[] GetSimpleImage() { return File.ReadAllBytes("static/small-image.png"); - //return ImageHelper.SimpleImage; } public byte[] GetBigImage() { return File.ReadAllBytes("static/big-image.png"); - //return ImageHelper.BigImage; } } } diff --git a/ActixAPI/Dockerfile b/ActixAPI/Dockerfile index d4f625f..dce9b80 100644 --- a/ActixAPI/Dockerfile +++ b/ActixAPI/Dockerfile @@ -1,25 +1,25 @@ -FROM rust:slim-bullseye AS build-env - -WORKDIR /app - -COPY . . - -RUN apt update && apt install wget -y && \ - wget https://files.ivanch.me/api/public/dl/Dj0gkp-m/small-image.png && \ - wget https://files.ivanch.me/api/public/dl/FqHEPM1Q/big-image.png && \ - wget https://files.ivanch.me/api/public/dl/nTAYqZwD/video.mp4 && \ - mv small-image.png ./static && \ - mv big-image.png ./static && \ - mv video.mp4 ./static - -RUN cargo build --release - -FROM debian:bullseye-slim - -WORKDIR /app - -COPY --from=build-env /app/target/release . - -COPY --from=build-env /app/static . - -ENTRYPOINT ["./ActixAPI"] \ No newline at end of file +FROM rust:slim-bullseye AS build-env + +WORKDIR /app + +COPY . . + +RUN apt update && apt install wget -y && \ + wget https://files.ivanch.me/api/public/dl/iFuXSNhw/small-image.png && \ + wget https://files.ivanch.me/api/public/dl/81Bkht5C/big-image.png && \ + wget https://files.ivanch.me/api/public/dl/nAndfAjK/video.mp4 && \ + mv small-image.png ./static && \ + mv big-image.png ./static && \ + mv video.mp4 ./static + +RUN cargo build --release + +FROM debian:bullseye-slim + +WORKDIR /app + +COPY --from=build-env /app/target/release . + +COPY --from=build-env /app/static ./static + +ENTRYPOINT ["./ActixAPI"] diff --git a/FlaskAPI/Dockerfile b/FlaskAPI/Dockerfile index fc531b4..2a22885 100644 --- a/FlaskAPI/Dockerfile +++ b/FlaskAPI/Dockerfile @@ -26,4 +26,4 @@ RUN --mount=type=cache,target=/root/.cache/pip \ EXPOSE 5000 # Run the application. -CMD gunicorn 'app:app' --bind=0.0.0.0:5000 +CMD gunicorn 'app:app' --bind=0.0.0.0:5000 --timeout 3600 diff --git a/FlaskAPI/app.py b/FlaskAPI/app.py index b048e75..c63e1c6 100644 --- a/FlaskAPI/app.py +++ b/FlaskAPI/app.py @@ -4,9 +4,9 @@ from controllers.image import image_blueprint app = Flask(__name__) -if __name__ == '__main__': - app.run() - # app.register_blueprint(status_blueprint) -app.register_blueprint(image_blueprint) \ No newline at end of file +app.register_blueprint(image_blueprint) + +if __name__ == '__main__': + app.run() diff --git a/FlaskAPI/controllers/image.py b/FlaskAPI/controllers/image.py index 33e100a..aac4dbc 100644 --- a/FlaskAPI/controllers/image.py +++ b/FlaskAPI/controllers/image.py @@ -1,11 +1,10 @@ +import io from services.image import ImageService -from static.image_helper import ImageHelper from flask import request, Response, Blueprint, jsonify, send_file image_blueprint = Blueprint('image_blueprint', __name__) image_service = ImageService() - @image_blueprint.route('/image/blur', methods=['POST']) def blur_image(): radius = int(request.args.get('radius')) @@ -22,7 +21,7 @@ def blur_image(): @image_blueprint.route('/image/load-small-image', methods=['GET']) def get_simple_image(): - return send_file(image_service.get_simple_image(), + return send_file(io.BytesIO(image_service.get_simple_image()), mimetype='image/png', as_attachment=True, download_name='small-image.png') @@ -30,7 +29,7 @@ def get_simple_image(): @image_blueprint.route('/image/load-big-image', methods=['GET']) def get_big_image(): - return send_file(image_service.get_big_image(), + return send_file(io.BytesIO(image_service.get_big_image()), mimetype='image/png', as_attachment=True, download_name='big-image.png') @@ -39,3 +38,5 @@ def get_big_image(): @image_blueprint.route('/image/save-big-image', methods=['POST']) def save_image(): image_service.save_image(request.get_data()) + + return "OK", 200 diff --git a/FlaskAPI/services/image.py b/FlaskAPI/services/image.py index f5a261f..e4caeac 100644 --- a/FlaskAPI/services/image.py +++ b/FlaskAPI/services/image.py @@ -1,30 +1,29 @@ from wand.image import Image -def box_blur_image_separable(image, blurred_image, radius_x, radius_y): - pixels = image.get_pixels() - blurred_pixels = blurred_image.get_pixels() +def box_blur_image_separable(image, radius_x, radius_y): + blurred_image = image.clone() + width, height = image.width, image.height - for pixel in pixels: - x, y = pixel[0], pixel[1] + 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 + 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 + 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_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) + 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 @@ -34,10 +33,9 @@ class ImageService: def box_blur_image(self, img, radius): with Image(blob=img) as image: - blurred_image = image.clone() - image = box_blur_image_separable(image, blurred_image, radius, 0) - image = box_blur_image_separable(image, blurred_image, 0, radius) - return image + 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): with open("./static/small-image.png", "rb") as file: @@ -50,3 +48,6 @@ class ImageService: def save_image(self, img): with open("image.png", "wb") as file: file.write(img) + + return True + diff --git a/FlaskAPI/static/image_helper.py b/FlaskAPI/static/image_helper.py deleted file mode 100644 index 55e9c8f..0000000 --- a/FlaskAPI/static/image_helper.py +++ /dev/null @@ -1,14 +0,0 @@ -from wand.image import Image - - -class ImageHelper: - SimpleImage = None - BigImage = None - - @staticmethod - def load_images(): - ImageHelper.SimpleImage = Image(filename="./static/small-image.png") - - ImageHelper.BigImage = Image(filename="./static/big-image.png") - -ImageHelper.load_images() \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index dc40e0f..6ec6e82 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,6 +8,11 @@ services: restart: always ports: - "9081:80" + deploy: + resources: + limits: + cpus: '1' + memory: 1GB tcc-flask: image: tcc:flask container_name: tcc-flask @@ -15,6 +20,11 @@ services: restart: always ports: - "9082:5000" + deploy: + resources: + limits: + cpus: '1' + memory: 1GB tcc-actix: image: tcc:actix container_name: tcc-actix @@ -22,3 +32,9 @@ services: restart: always ports: - "9083:9090" + deploy: + resources: + limits: + cpus: '1' + memory: 1GB +