如何在Python中提取图像元数据?代码实现教程

2021年11月16日15:03:42 发表评论 629 次浏览

Python如何提取图像元数据?本文带你了解如何使用带有 Pillow 库的 Python 中的可交换图像文件格式 (EXIF) 提取图像元数据,例如 GPS 信息、相机品牌、型号等。

如何在Python中提取图像元数据?在本教程中,你将学习如何使用Python 中的Pillow 库在图像中提取一些有用的元数据。

数码相机、智能手机和扫描仪等设备使用EXIF 标准来保存图像或音频文件。该标准包含许多可用于取证调查的有用标签可供提取,例如设备的品牌、型号、图像创建的确切日期和时间,甚至某些设备上的 GPS 信息。

请注意,Linux 上有免费的元数据提取工具,例如 ImageMagick 或 ExifTool,本教程的目标是使用 Python 编程语言提取元数据。

相关: 如何在 Python 中提取 PDF 元数据

Python提取图像元数据示例 - 首先,你需要安装 Pillow 库:

pip3 install Pillow

打开一个新的 Python 文件并按照以下步骤操作:

from PIL import Image
from PIL.ExifTags import TAGS

现在这仅适用于JPEG图像文件,拍摄你拍摄的任何图像并在本教程中对其进行测试(如果你想在我的图像上进行测试,你可以在教程的存储库中找到它):

# path to the image or video
imagename = "image.jpg"

# read the image data using PIL
image = Image.open(imagename)

使用Image.open()函数读取图像后,让我们调用返回图像元数据的图像的getexif()方法:

# extract EXIF data
exifdata = image.getexif()

Python提取图像元数据示例 - exifdata现在变量的问题是字段名称只是 ID,而不是人类可读的字段名称,这就是为什么我们需要来自PIL.ExifTags模块的TAGS字典,它将每个标签 ID 映射到人类可读的文本中:

# iterating over all EXIF data fields
for tag_id in exifdata:
    # get the tag name, instead of human unreadable tag id
    tag = TAGS.get(tag_id, tag_id)
    data = exifdata.get(tag_id)
    # decode bytes 
    if isinstance(data, bytes):
        data = data.decode()
    print(f"{tag:25}: {data}")

Python如何提取图像元数据?这是我的输出:

ExifVersion              : 0220
ShutterSpeedValue        : (406, 100)
ApertureValue            : (1851, 1000)       
DateTimeOriginal         : 2016:11:10 18:22:47
DateTimeDigitized        : 2016:11:10 18:22:47
BrightnessValue          : (-109, 100)        
ExposureBiasValue        : (0, 10)
MaxApertureValue         : (1851, 1000)       
MeteringMode             : 2
Flash                    : 0
FocalLength              : (220, 100)
UserComment              :
ColorSpace               : 1
ExifImageWidth           : 2592
FocalLengthIn35mmFilm    : 22
SceneCaptureType         : 0
ExifImageHeight          : 1944
ImageWidth               : 2592
ImageLength              : 1944
Make                     : samsung
Model                    : SM-G920F
Orientation              : 8
YCbCrPositioning         : 1
ExposureTime             : (1, 17)
XResolution              : (72, 1)
YResolution              : (72, 1)
FNumber                  : (19, 10)
FNumber                  : (19, 10)
ImageUniqueID            : B05LLHA01PM
ISOSpeedRatings          : 400
ISOSpeedRatings          : 400
ISOSpeedRatings          : 400
ISOSpeedRatings          : 400
ResolutionUnit           : 2
ExifOffset               : 226
ExposureMode             : 0
FlashPixVersion          : 0100
WhiteBalance             : 0
Software                 : G920FXXS4DPI4
DateTime                 : 2016:11:10 18:22:4
MakerNote                :       0100                      Z   @         P          
                                                           Z   @         P

如何在Python中提取图像元数据?以上是一堆有用的东西,通过快速搜索模型,我得出结论,这张图像是由三星 Galaxy S6 拍摄的,在其他设备拍摄的图像上运行它,你会看到不同的(可能更多)字段。

好的,我们完成了。对你来说一个很好的挑战是从 URL 下载所有图像,然后在你找到的每个图像上运行本教程的脚本并调查有趣的结果!

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: