File tree 2 files changed +51
-0
lines changed
2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change @@ -86,6 +86,7 @@ def decode_base64_to_image(encoding):
86
86
response = requests .get (encoding , timeout = 30 , headers = headers )
87
87
try :
88
88
image = Image .open (BytesIO (response .content ))
89
+ image = images .apply_exif_orientation (image )
89
90
return image
90
91
except Exception as e :
91
92
raise HTTPException (status_code = 500 , detail = "Invalid image url" ) from e
@@ -94,6 +95,7 @@ def decode_base64_to_image(encoding):
94
95
encoding = encoding .split (";" )[1 ].split ("," )[1 ]
95
96
try :
96
97
image = Image .open (BytesIO (base64 .b64decode (encoding )))
98
+ image = images .apply_exif_orientation (image )
97
99
return image
98
100
except Exception as e :
99
101
raise HTTPException (status_code = 500 , detail = "Invalid encoded image" ) from e
Original file line number Diff line number Diff line change @@ -800,3 +800,52 @@ def flatten(img, bgcolor):
800
800
801
801
return img .convert ('RGB' )
802
802
803
+
804
+ # https://www.exiv2.org/tags.html
805
+ _EXIF_ORIENT = 274 # exif 'Orientation' tag
806
+
807
+ def apply_exif_orientation (image ):
808
+ """
809
+ Applies the exif orientation correctly.
810
+
811
+ This code exists per the bug:
812
+ https://github.com/python-pillow/Pillow/issues/3973
813
+ with the function `ImageOps.exif_transpose`. The Pillow source raises errors with
814
+ various methods, especially `tobytes`
815
+
816
+ Function based on:
817
+ https://github.com/wkentaro/labelme/blob/v4.5.4/labelme/utils/image.py#L59
818
+ https://github.com/python-pillow/Pillow/blob/7.1.2/src/PIL/ImageOps.py#L527
819
+
820
+ Args:
821
+ image (PIL.Image): a PIL image
822
+
823
+ Returns:
824
+ (PIL.Image): the PIL image with exif orientation applied, if applicable
825
+ """
826
+ if not hasattr (image , "getexif" ):
827
+ return image
828
+
829
+ try :
830
+ exif = image .getexif ()
831
+ except Exception : # https://github.com/facebookresearch/detectron2/issues/1885
832
+ exif = None
833
+
834
+ if exif is None :
835
+ return image
836
+
837
+ orientation = exif .get (_EXIF_ORIENT )
838
+
839
+ method = {
840
+ 2 : Image .FLIP_LEFT_RIGHT ,
841
+ 3 : Image .ROTATE_180 ,
842
+ 4 : Image .FLIP_TOP_BOTTOM ,
843
+ 5 : Image .TRANSPOSE ,
844
+ 6 : Image .ROTATE_270 ,
845
+ 7 : Image .TRANSVERSE ,
846
+ 8 : Image .ROTATE_90 ,
847
+ }.get (orientation )
848
+
849
+ if method is not None :
850
+ return image .transpose (method )
851
+ return image
You can’t perform that action at this time.
0 commit comments