File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 33import time
44import requests
55
6- from PIL import Image , ImageFile
6+ from PIL import Image , ImageFile , ImageOps
77from src import TMP_FOLDER
88from 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+
109123def resize (image_path , target_dimensions , image_format ):
110124 """
111125 Resize image given the target dimensions, saving it in the same file.
You can’t perform that action at this time.
0 commit comments