跳转到内容

Python 3:处理文件和数字资产/图像/EXIF 数据

来自维基教科书,开放的书籍,开放的世界

EXIF 数据

[编辑 | 编辑源代码]
%pip install pillow


pip install pillow

数据检索

[编辑 | 编辑源代码]

代码片段[1]

from PIL import Image
from PIL.ExifTags import TAGS as exif_tags

# location of the image you want to extract the exif information from
location_of_file: str = ''

def retrievement_of_exif_table(
        exif_information
    ) -> dict:
    exif_table: dict = {}

    for key, value in exif_information.items():
        exif_label = exif_tags.get(key)
                
        if not(
            exif_label is None
        ):
            exif_table[exif_label] = value
    
    return exif_table

exif_table = {}

with Image.open(location_of_file) as image:
    exif_table = retrievement_of_exif_table(
        image.getexif()
    )

GPS 位置检索

[编辑 | 编辑源代码]

代码片段[1]

from PIL.ExifTags import GPS, GPSTAGS

# .....
def convert_gps_information(
    information
) -> dict:
    gps_information: dict = {}

    for key in information.keys():
        gps_label = GPSTAGS.get(key)
        gps_information[gps_label] = information[key]

    return gps_information


def retrievement_of_exif_table(
        exif_information
    ) -> dict:
    # .....
    for key, value in exif_information.items():
        exif_label = exif_tags.get(key)
        
        if exif_label == 'GPSInfo':
            value = convert_gps_information(
                exif_information.get_ifd(
                    key
                )
            )
    # .....


  1. a b "image exif information tutorial.ipynb". Gist. Retrieved 2023-07-12.
华夏公益教科书