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) 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.