butterfly_viewer.aux_exif

Functions which use image EXIF for Butterfly Viewer and Registrator.

Not intended as a script.

 1#!/usr/bin/env python3
 2
 3"""Functions which use image EXIF for Butterfly Viewer and Registrator.
 4
 5Not intended as a script.
 6"""
 7# SPDX-License-Identifier: GPL-3.0-or-later
 8
 9import piexif
10
11
12
13def get_exif_rotation_angle(filepath):
14    """Get rotation angle from EXIF of image file.
15    
16    Credit: tutuDajuju
17
18    Args:
19        filepath (str): Absolute path of image file.
20
21    Returns:
22        orientation (int or None): Image orientation as integer angle if exists; None if does not exist.
23    """
24    try:
25        exif_dict = piexif.load(filepath)
26    except:
27        return None
28    else:
29        try:
30            exif_dict["0th"]
31        except:
32            return None
33        else:
34            if piexif.ImageIFD.Orientation in exif_dict["0th"]:
35                orientation = exif_dict["0th"][piexif.ImageIFD.Orientation]
36                if orientation == 3:
37                    return 180
38                elif orientation == 6:
39                    return 90
40                elif orientation == 8:
41                    return 270
42                else:
43                    return None
44            else:
45                return None
def get_exif_rotation_angle(filepath):
14def get_exif_rotation_angle(filepath):
15    """Get rotation angle from EXIF of image file.
16    
17    Credit: tutuDajuju
18
19    Args:
20        filepath (str): Absolute path of image file.
21
22    Returns:
23        orientation (int or None): Image orientation as integer angle if exists; None if does not exist.
24    """
25    try:
26        exif_dict = piexif.load(filepath)
27    except:
28        return None
29    else:
30        try:
31            exif_dict["0th"]
32        except:
33            return None
34        else:
35            if piexif.ImageIFD.Orientation in exif_dict["0th"]:
36                orientation = exif_dict["0th"][piexif.ImageIFD.Orientation]
37                if orientation == 3:
38                    return 180
39                elif orientation == 6:
40                    return 90
41                elif orientation == 8:
42                    return 270
43                else:
44                    return None
45            else:
46                return None

Get rotation angle from EXIF of image file.

Credit: tutuDajuju

Arguments:
  • filepath (str): Absolute path of image file.
Returns:
  • orientation (int or None): Image orientation as integer angle if exists; None if does not exist.