From 84df834b82389a32a90f2b92b4e64a32e5fd2735 Mon Sep 17 00:00:00 2001 From: dede- Date: Sun, 17 Sep 2023 21:55:40 -0300 Subject: [PATCH] Add load image, Dockerfile e requirements --- FlaskAPI/Dockerfile | 45 +++++++++++++++++++++++++++++++++ FlaskAPI/controllers/image.py | 18 ++++++++----- FlaskAPI/requirements.txt | 3 +++ FlaskAPI/services/image.py | 34 +++++++++++++++++-------- FlaskAPI/static/image_helper.py | 10 +++++--- 5 files changed, 88 insertions(+), 22 deletions(-) create mode 100644 FlaskAPI/Dockerfile create mode 100644 FlaskAPI/requirements.txt diff --git a/FlaskAPI/Dockerfile b/FlaskAPI/Dockerfile new file mode 100644 index 0000000..b6a5924 --- /dev/null +++ b/FlaskAPI/Dockerfile @@ -0,0 +1,45 @@ +# syntax=docker/dockerfile:1 + +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Dockerfile reference guide at +# https://docs.docker.com/engine/reference/builder/ + +ARG PYTHON_VERSION=3.10.12 +FROM python:${PYTHON_VERSION}-slim as base + +# Prevents Python from writing pyc files. +ENV PYTHONDONTWRITEBYTECODE=1 + +# Keeps Python from buffering stdout and stderr to avoid situations where +# the application crashes without emitting any logs due to buffering. +ENV PYTHONUNBUFFERED=1 + +WORKDIR /app + +# Copy the source code into the container. +COPY . . + +RUN apt-get update && apt-get install -y imagemagick && apt-get install -y wget && ls + +RUN 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 && \ + rm -rf runtimes && \ + mkdir -p ./static && \ + mv small-image.png ./static && \ + mv big-image.png ./static && \ + mv video.mp4 ./static + +# Download dependencies as a separate step to take advantage of Docker's caching. +# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds. +# Leverage a bind mount to requirements.txt to avoid having to copy them into +# into this layer. +RUN --mount=type=cache,target=/root/.cache/pip \ + --mount=type=bind,source=requirements.txt,target=requirements.txt \ + python -m pip install -r requirements.txt + +# Expose the port that the application listens on. +EXPOSE 5000 + +# Run the application. +CMD gunicorn 'app:app' --bind=0.0.0.0:5000 diff --git a/FlaskAPI/controllers/image.py b/FlaskAPI/controllers/image.py index 19322be..4083088 100644 --- a/FlaskAPI/controllers/image.py +++ b/FlaskAPI/controllers/image.py @@ -1,4 +1,3 @@ -import io from services.image import ImageService from static.image_helper import ImageHelper from flask import request, Response, Blueprint, jsonify, send_file @@ -18,20 +17,25 @@ def blur_image(): as_attachment=True, download_name='blurred_image.jpeg') - return jsonify(status=400, message="Bad request") + return "Bad request", 400 -@image_blueprint.route('/image/load-image', methods=['POST']) +@image_blueprint.route('/image/load-small-image', methods=['GET']) def get_simple_image(): - pass + return send_file(image_service.get_simple_image(), + mimetype='image/png', + as_attachment=True, + download_name='small-image.png') @image_blueprint.route('/image/load-big-image', methods=['GET']) def get_big_image(): - pass + return send_file(image_service.get_big_image(), + mimetype='image/png', + as_attachment=True, + download_name='big-image.png') @image_blueprint.route('/image/save-big-image', methods=['POST']) -def save_big_image(): +def save_image(): pass - diff --git a/FlaskAPI/requirements.txt b/FlaskAPI/requirements.txt new file mode 100644 index 0000000..20ce748 --- /dev/null +++ b/FlaskAPI/requirements.txt @@ -0,0 +1,3 @@ +Flask>=1.0 +gunicorn>=19.9.0 +Wand \ No newline at end of file diff --git a/FlaskAPI/services/image.py b/FlaskAPI/services/image.py index 35da64c..6dea1da 100644 --- a/FlaskAPI/services/image.py +++ b/FlaskAPI/services/image.py @@ -1,5 +1,7 @@ from wand.image import Image +from static.image_helper import ImageHelper + def box_blur_image_separable(image, blurred_image, radius_x, radius_y): pixels = image.get_pixels() @@ -30,28 +32,38 @@ def box_blur_image_separable(image, blurred_image, radius_x, radius_y): return blurred_image +def save_image(file_stream): + with open("image.png", "wb") as file: + file.write(file_stream.read()) + file.close() + + class ImageService: def __init__(self): pass - def box_blur_image(self, image, radius): + def box_blur_image(self, img, radius): temp_path = 'temp_image.png' - image.save(temp_path) + img.save(temp_path) with Image(filename=temp_path) as img: - img.blur(radius, 2) # Apply blur directly to the image object + img.blur(radius, 2) blurred_temp_path = 'blurred_temp_image.png' - img.save(filename=blurred_temp_path) # Save the modified image + img.save(filename='blurred_temp_image.png') return blurred_temp_path - def save_image(self, file_stream): - with open("image.png", "wb") as file: - file.write(file_stream.read()) - file.close() - def get_simple_image(self): - return Image(filename='path_to_simple_image.png') + with ImageHelper.SimpleImage as img: + img = ImageHelper.SimpleImage + simple_image = 'simple_image.png' + img.save(filename='simple_image.png') + return simple_image + def get_big_image(self): - return Image(filename='path_to_big_image.png') + with ImageHelper.BigImage as img: + img = ImageHelper.BigImage + big_image = 'big_image.png' + img.save(filename='big_image.png') + return big_image diff --git a/FlaskAPI/static/image_helper.py b/FlaskAPI/static/image_helper.py index d566946..63aba27 100644 --- a/FlaskAPI/static/image_helper.py +++ b/FlaskAPI/static/image_helper.py @@ -7,9 +7,11 @@ class ImageHelper: @staticmethod def load_images(): - # ImageHelper.SimpleImage = Image(filename="simpleimage.png") - # ImageHelper.BigImage = Image(filename="bigimage.png") + ImageHelper.SimpleImage = Image(filename="./static/small-image.png") + #ImageHelper.SimpleImage.save(filename="./static/small-image.png") + + ImageHelper.BigImage = Image(filename="./static/big-image.png") + #ImageHelper.BigImage.save(filename="./static/big-image.png") pass - -ImageHelper.load_images() +ImageHelper.load_images() \ No newline at end of file