Skip to content

Commit 5bf8d19

Browse files
authored
Merge pull request #8 from AlbertSuarez/rotation_based_on_metadata
Rotation based on metadata
2 parents 2e9102e + 70bb5c3 commit 5bf8d19

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

multiplexer/src/api/remove.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def post():
4343
if not image_path:
4444
return make_response(correlation_id, True, error_id='002')
4545

46+
with Timer('Rotate image, if needed'):
47+
image.rotate(image_path)
48+
4649
with Timer('Extract image dimensions'):
4750
original_dimensions = image.get_dimensions(image_path)
4851

multiplexer/src/utils/image.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import time
44
import requests
55

6-
from PIL import Image, ImageFile
6+
from PIL import Image, ImageFile, ImageOps
77
from src import TMP_FOLDER
88
from src.utils import log, storage
99

@@ -106,6 +106,20 @@ def get_dimensions(image_path):
106106
return img.size
107107

108108

109+
def rotate(image_path):
110+
"""
111+
Rotate an image based on meta-data from its path.
112+
:param image_path: Image path to retrieve dimensions.
113+
:return: Image rotated.
114+
"""
115+
try:
116+
with Image.open(image_path) as img:
117+
img = ImageOps.exif_transpose(img)
118+
img.save(image_path, format=img.format, quality=95)
119+
except Exception as e:
120+
log.warn(f'Cannot rotate input image: [{e}]')
121+
122+
109123
def resize(image_path, target_dimensions, image_format):
110124
"""
111125
Resize image given the target dimensions, saving it in the same file.

0 commit comments

Comments
 (0)