python3 getImgDetail_PIL_exifread.py

"""
模块:python3 getImgDetail_PIL_exifread.py
功能:python3 获取一个照片的详细信息。
参考:https://blog.csdn.net/devil8123665/article/details/104831227/
1.PIL.Image.open(fp, mode='r')
    打开并标识给定的图像文件。

    这是一种懒惰的操作;
    此函数标识该文件,但该文件保持打开状态,
    在尝试处理数据(或者调用方法:py:meth:`~PIL.Image.Image.load`)之前,
    不会从该文件读取实际图像数据。
    See :py:func:`~PIL.Image.new`.
    See :ref:`file-handling`.

    :param fp: 一个文件名 (字符串), pathlib.Path 对象或一个文件对象。
       文件对象必须实现方法:
       :py:meth:`~file.read`, :py:meth:`~file.seek`, 和 :py:meth:`~file.tell`,
       并且以二进制模式打开。
    :param mode: 模式。如果指定,这个参数必须是 "r"。
    :returns: 一个 :py:class:`~PIL.Image.Image` 对象。
    :exception FileNotFoundError: 如果文件未找到。
    :exception PIL.UnidentifiedImageError: 如果图片不能打开并标识。
    :exception ValueError: If the ``mode`` is not "r", or if a ``StringIO``
       instance is used for ``fp``.
"""
from PIL import Image
from PIL.ExifTags import TAGS
import exifread


def readImgExif(imgFilepath):
    d1, d2 = {}, {}
    # 1.使用 Image.open().
    with Image.open(imgFilepath) as imgFile:
        # 打开图片。
        # imgFile.show()
        # print(type(imgFile))
        # <class 'PIL.JpegImagePlugin.JpegImageFile'>
        for (k, v) in imgFile._getexif().items():
            # print('%s : %s' % (TAGS.get(k), v))
            d1[TAGS.get(k)] = v
    print("d1:", d1)
    # d1: {'ExifVersion': b'0220', None: 'E',
    # 'GPSInfo': {0: b'\x02\x02\x00\x00', 1: 'N', 2: ((39, 1), (1, 1), (5984939, 100000)),
    # 4: ((117, 1), (12, 1), (3505979, 100000)), 5: b'\x00',
    # 6: (2500, 1000), 7: ((3, 1), (2, 1), (5400, 100)), 12: 'K',
    # 13: (526, 100), 29: '0015:02:12'}, 'ExifOffset': 126,
    # 'Make': 'Point Grey Research', 'Model': 'Ladybug', 'Software': 'Ladybug SDK'}
    print()

    # 2.使用 exifread.process_file().
    with open(imgFilepath, 'rb') as imgFile:
        imgDetail = exifread.process_file(imgFile)
    for tag in imgDetail:
        d2[tag] = imgDetail[tag]
    print("d2:", d2)
    # d2: {'Image Make': (0x010F) ASCII=Point Grey Research @ 86,
    # 'Image Model': (0x0110) ASCII=Ladybug @ 106,
    # 'Image Software': (0x0131) ASCII=Ladybug SDK @ 114,
    # 'GPS GPSVersionID': (0x0000) Byte=[2, 2, 0, 0] @ 150,
    # 'GPS GPSLatitudeRef': (0x0001) ASCII=N @ 162,
    # 'GPS GPSLatitude': (0x0002) Ratio=[39, 1, 5984939/100000] @ 262,
    # 'GPS GPSLongitude': (0x0004) Ratio=[117, 12, 3505979/100000] @ 286,
    # 'GPS GPSAltitudeRef': (0x0005) Byte=0 @ 198,
    # 'GPS GPSAltitude': (0x0006) Ratio=5/2 @ 310,
    # 'GPS GPSSpeedRef': (0x000C) ASCII=K @ 222,
    # 'GPS GPSSpeed': (0x000D) Ratio=263/50 @ 318,
    # 'GPS GPSTimeStamp': (0x0007) Ratio=[3, 2, 54] @ 326,
    # 'GPS GPSDate': (0x001D) ASCII=0015:02:12 @ 350,
    # 'Image GPSInfo': (0x8825) Long=140 @ 54,
    # 'Image ExifOffset': (0x8769) Long=126 @ 66,
    # 'Image Tag 0x0003': (0x0003) ASCII=E @ 78,
    # 'EXIF ExifVersion': (0x9000) Undefined=0220 @ 136}


if __name__ == '__main__':
    imgFilepath = r'F:\Gaoshengjie\LongGeTasks\streetView\output\waihuanlu\ladybug_panoramic_000091.jpg'
    # GPSInfo: {0: b'\x02\x02\x00\x00', 1: 'N', 2: ((39, 1), (1, 1), (5984939, 100000)), 4: ((117, 1), (12, 1), (3505979, 100000)), 5: b'\x00', 6: (2500, 1000), 7: ((3, 1), (2, 1), (5400, 100)), 12: 'K', 13: (526, 100), 29: '0015:02:12'}
    readImgExif(imgFilepath)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值