跳轉到內容

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.
華夏公益教科書