@@ -34,25 +34,28 @@ def set_orientation(info: dict, orientation: int = 1) -> Union[int, None]:
3434
3535 original_orientation = None
3636 if info .get ("exif" , None ):
37- tif_tag = info ["exif" ]
38- if tif_tag .startswith (b"Exif\x00 \x00 " ):
39- tif_tag = tif_tag [6 :]
40- endian_mark = "<" if tif_tag [0 :2 ] == b"\x49 \x49 " else ">"
41- pointer = unpack (endian_mark + "L" , tif_tag [4 :8 ])[0 ]
42- tag_count = unpack (endian_mark + "H" , tif_tag [pointer : pointer + 2 ])[0 ]
43- offset = pointer + 2
44- for tag_n in range (tag_count ):
45- pointer = offset + 12 * tag_n
46- if unpack (endian_mark + "H" , tif_tag [pointer : pointer + 2 ])[0 ] != 274 :
47- continue
48- value = tif_tag [pointer + 8 : pointer + 12 ]
49- _original_orientation = unpack (endian_mark + "H" , value [0 :2 ])[0 ]
50- if _original_orientation != 1 :
51- original_orientation = _original_orientation
52- p_value = 6 + pointer + 8
53- new_orientation = pack (endian_mark + "H" , orientation )
54- info ["exif" ] = info ["exif" ][:p_value ] + new_orientation + info ["exif" ][p_value + 2 :]
55- break
37+ try :
38+ tif_tag = info ["exif" ]
39+ if tif_tag .startswith (b"Exif\x00 \x00 " ):
40+ tif_tag = tif_tag [6 :]
41+ endian_mark = "<" if tif_tag [0 :2 ] == b"\x49 \x49 " else ">"
42+ pointer = unpack (endian_mark + "L" , tif_tag [4 :8 ])[0 ]
43+ tag_count = unpack (endian_mark + "H" , tif_tag [pointer : pointer + 2 ])[0 ]
44+ offset = pointer + 2
45+ for tag_n in range (tag_count ):
46+ pointer = offset + 12 * tag_n
47+ if unpack (endian_mark + "H" , tif_tag [pointer : pointer + 2 ])[0 ] != 274 :
48+ continue
49+ value = tif_tag [pointer + 8 : pointer + 12 ]
50+ _original_orientation = unpack (endian_mark + "H" , value [0 :2 ])[0 ]
51+ if _original_orientation != 1 :
52+ original_orientation = _original_orientation
53+ p_value = 6 + pointer + 8
54+ new_orientation = pack (endian_mark + "H" , orientation )
55+ info ["exif" ] = info ["exif" ][:p_value ] + new_orientation + info ["exif" ][p_value + 2 :]
56+ break
57+ except Exception : # noqa # pylint: disable=broad-except
58+ pass
5659 if info .get ("xmp" , None ):
5760 xmp_data = info ["xmp" ].decode ("utf-8" )
5861 match = re .search (r'tiff:Orientation(="|>)([0-9])' , xmp_data )
0 commit comments