From c366636fb56a43777d63734df7a7e5e27ac5e941 Mon Sep 17 00:00:00 2001 From: Albert Suarez Date: Fri, 9 Apr 2021 10:39:23 +0200 Subject: [PATCH 1/2] :sparkles: Implemented image util function for rotating based on meta-data --- multiplexer/src/utils/image.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/multiplexer/src/utils/image.py b/multiplexer/src/utils/image.py index 185c188..c0c9fe4 100644 --- a/multiplexer/src/utils/image.py +++ b/multiplexer/src/utils/image.py @@ -3,7 +3,7 @@ import time import requests -from PIL import Image, ImageFile +from PIL import Image, ImageFile, ImageOps from src import TMP_FOLDER from src.utils import log, storage @@ -106,6 +106,20 @@ def get_dimensions(image_path): return img.size +def rotate(image_path): + """ + Rotate an image based on meta-data from its path. + :param image_path: Image path to retrieve dimensions. + :return: Image rotated. + """ + try: + with Image.open(image_path) as img: + img = ImageOps.exif_transpose(img) + img.save(image_path, format=img.format, quality=95) + except Exception as e: + log.warn(f'Cannot rotate input image: [{e}]') + + def resize(image_path, target_dimensions, image_format): """ Resize image given the target dimensions, saving it in the same file. From 70bb5c3bf55becb5add4af06b022f3ec478af84a Mon Sep 17 00:00:00 2001 From: Albert Suarez Date: Fri, 9 Apr 2021 10:39:41 +0200 Subject: [PATCH 2/2] :sparkles: Added rotation into the workflow after the download --- multiplexer/src/api/remove.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/multiplexer/src/api/remove.py b/multiplexer/src/api/remove.py index 2f210c3..952f72f 100644 --- a/multiplexer/src/api/remove.py +++ b/multiplexer/src/api/remove.py @@ -43,6 +43,9 @@ def post(): if not image_path: return make_response(correlation_id, True, error_id='002') + with Timer('Rotate image, if needed'): + image.rotate(image_path) + with Timer('Extract image dimensions'): original_dimensions = image.get_dimensions(image_path)