Skip to content

Commit

Permalink
Merge pull request #8 from AlbertSuarez/rotation_based_on_metadata
Browse files Browse the repository at this point in the history
Rotation based on metadata
  • Loading branch information
adriacabeza authored Apr 9, 2021
2 parents 2e9102e + 70bb5c3 commit 5bf8d19
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions multiplexer/src/api/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
16 changes: 15 additions & 1 deletion multiplexer/src/utils/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 5bf8d19

Please sign in to comment.