image-processing - 如何显示有关图像的所有元数据?

标签 image-processing python-imaging-library exif

我正在尝试使用 Pillow 的 TAGS 功能读取图像的元数据。我使用以下代码来获取此信息:

# imports
from PIL import Image
from PIL.ExifTags import TAGS
    
imagename = 'image.jpg'
image = Image.open(imagename)

exifdata = image.getexif()
for tag_id in exifdata:
    tag = TAGS.get(tag_id,tag_id)
    print(tag_id)
    data = exifdata.get(tag_id)
    # we decode bytes
    if isinstance(data,bytes):
        data = data.decode()
    print(f'{tag:25}: {data}')

我得到的结果类似于以下输出:

ImageWidth               : 4128
ImageLength              : 1908
ResolutionUnit           : 2
ExifOffset               : 226
Make                     : samsung
Model                    : SM-M205F
Software                 : M205FDDS8CUC1
Orientation              : 8
DateTime                 : 2021:06:18 12:08:37
YCbCrPositioning         : 1
XResolution              : 72.0
YResolution              : 72.0

问题是我需要的关键数据不在列表中。例如,焦距、测光模式、曝光时间以及其他指标和详细信息不存在,但我可以通过右键单击“属性”部分中的图像来查看它们,如下图所示:

metadata from properties

为什么 Pillow 的 TAGS 无法读取所有详细信息。有什么方法可以让它获得所需的所有详细信息吗?

最佳答案

您提到的属性 - 焦距、测光模式和曝光时间 - 均在 EXIF IFD 内 - https://www.awaresystems.be/imaging/tiff/tifftags/privateifd/exif.html

自 Pillow 8.2.0 起,getexif() 仅返回顶级标签。要获取 EXIF IFD 中的数据,您需要使用 get_ifd()

要从此处的其他答案中获取一些代码并进行修改,

from PIL import Image
from PIL.ExifTags import TAGS

def print_exif_data(exif_data):
    for tag_id in exif_data:
        tag = TAGS.get(tag_id, tag_id)
        content = exif_data.get(tag_id)
        print(f'{tag:25}: {content}')

with Image.open("Tests/images/flower.jpg") as im:
    exif = im.getexif()
    
    print_exif_data(exif)
    print()
    print_exif_data(exif.get_ifd(0x8769))

我明白了

ResolutionUnit           : 2
ExifOffset               : 196
Make                     : Canon
Model                    : Canon PowerShot S40
Orientation              : 1
DateTime                 : 2003:12:14 12:01:44
YCbCrPositioning         : 1
XResolution              : 180.0
YResolution              : 180.0

ExifVersion              : b'0220'
ComponentsConfiguration  : b'\x01\x02\x03\x00'
CompressedBitsPerPixel   : 5.0
DateTimeOriginal         : 2003:12:14 12:01:44
DateTimeDigitized        : 2003:12:14 12:01:44
ShutterSpeedValue        : 8.96875
ApertureValue            : 4.65625
ExposureBiasValue        : 0.0
MaxApertureValue         : 2.970855712890625
MeteringMode             : 2
Flash                    : 24
FocalLength              : 21.3125
UserComment              : b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
ColorSpace               : 1
ExifImageWidth           : 2272
FocalPlaneXResolution    : 8114.285714285715
ExifImageHeight          : 1704
FocalPlaneYResolution    : 8114.285714285715
FocalPlaneResolutionUnit : 2
SensingMethod            : 2
FileSource               : b'\x03'
ExposureTime             : 0.002
ExifInteroperabilityOffset: 1416
FNumber                  : 4.9
CustomRendered           : 0
ExposureMode             : 0
FlashPixVersion          : b'0100'
WhiteBalance             : 0
DigitalZoomRatio         : 1.0
MakerNote                : b'\x0c\x00\x01\x00\x03\x00(\x00\x00\x00D\x04\x00\x00\x02\x00\x03\x00\x04\x00\x00\x00\x94\x04\x00\x00\x03\x00\x03\x00\x04\x00\x00\x00\x9c\x04\x00\x00\x04\x00\x03\x00\x1b\x00\x00\x00\xa4\x04\x00\x00\x00\x00\x03\x00\x06\x00\x00\x00\xda\x04\x00\x00\x00\x00\x03\x00\x04\x00\x00\x00\xe6\x04\x00\x00\x06\x00\x02\x00 \x00\x00\x00\xee\x04\x00\x00\x07\x00\x02\x00\x18\x00\x00\x00\x0e\x05\x00\x00\x08\x00\x04\x00\x01\x00\x00\x00;\xe1\x11\x00\t\x00\x02\x00 \x00\x00\x00&\x05\x00\x00\x10\x00\x04\x00\x01\x00\x00\x00\x00\x00\x11\x01\r\x00\x03\x00\x15\x00\x00\x00F\x05\x00\x00\x00\x00\x00\x00P\x00\x02\x00\x00\x00\x05\x00\x01\x00\x00\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x00\x05\x00\x01\x00\x030\x01\x00\xff\xff\xff\xff\xaa\x02\xe3\x00 \x00\x95\x00\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff1\x00\xe0\x08\xe0\x08\x00\x00\x01\x00\x02\x00\xaa\x02\x1e\x01\xd7\x00\x00\x00\x00\x00\x00\x00\x00\x006\x00\x00\x00\xa0\x00\x14\x01\x95\x00\x1f\x01\x00\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x020\x00\x00\x00\x00\x00\x00\x01\x00\x0e\x03\x00\x00\x95\x00!\x01\x00\x00\x00\x00\x00\x00\xfa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00IMG:PowerShot S40 JPEG\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Firmware Version 1.10\x00\x00\x00Andreas Huggel\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00*\x00\x03\x00\x01\x80z\x01\x01\x80\x00\x00\x00\x00\x00\x00\x03\x01\x02\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x009\x00\xc6\x00\x05\x00\x00\x00\x00\x00\x00\x00'
SceneCaptureType         : 0

关于image-processing - 如何显示有关图像的所有元数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68033479/

相关文章:

image - 如何为视频生成图像序列

c# - 如何将移动图像转换为黑白C#

python - 在 Python 的 PIL 中渲染波斯语(波斯语)单词

image - 如何在不改变像素值的情况下对图像数组进行采样

c++ - 是否有任何示例代码可以从 Jpeg exif header 中读取缩略图?

html - 如何在每次 Canvas 变换后实现 Lanczos 重采样而无需制作新 Canvas ?

python-2.7 - 指纹增强

python - PIL : scale image while maintaing highest possible quality

objective-c - 在 Objective-c 中从 exif 获取相机和镜头

php - 使用 PHP exif_read_data 获取照片的 "Taken Date"而不是 "Modified Date"