如何使用python从图像中提取元数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21697645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:25:50  来源:igfitidea点击:

How to extract metadata from a image using python?

pythonimagemetadataextract

提问by MrDanger

Hi im working on a program that will open an image and then extract the metadata from it How do i extract metadata using python ?

嗨,我正在开发一个可以打开图像然后从中提取元数据的程序我如何使用 python 提取元数据?

Thanks

谢谢

回答by Mzzl

Use Pillow, it's a fork of PIL that is still in active development, and supports python3. Here I use a dict generator to map the exif data to a dict

使用Pillow,它是仍在积极开发中的 PIL 的一个分支,并且支持 python3。这里我使用dict生成器将exif数据映射到dict

from PIL import Image, ExifTags
img = Image.open("/path/to/file.jpg")
exif = { ExifTags.TAGS[k]: v for k, v in img._getexif().items() if k in ExifTags.TAGS }

回答by Lasith Niroshan

You can use following python code for this.

您可以为此使用以下 python 代码。

#!/bin/python
import os
import sys
from PIL import Image
from PIL.ExifTags import TAGS

image = sys.argv[1]

for (tag,value) in Image.open(image)._getexif().iteritems():
        print '%s = %s' % (TAGS.get(tag), value)

Here is the sample output. enter image description here

这是示例输出。 在此处输入图片说明

回答by Vaibhav K

There is couple of ways by which you can get the data from the file.

您可以通过多种方式从文件中获取数据。

1. using Exif tool :

1. 使用 Exif 工具:

for Windows and mac: it is available at ExifToolfor linux : You can install ExifTool on Ubuntu using the apt utility sudo apt install libimage-exiftool-perl

对于 Windows 和 mac:它可以在 ExifToolfor linux 上获得:您可以使用 apt 实用程序在 Ubuntu 上安装 ExifTool sudo apt install libimage-exiftool-perl

You can now run ExifTool anywhere in your terminal by typing exiftool. This is recommended approach to get the meta data as it gives more tags than any other way. It is a command line tool and to use it in Python user have to create a subprocess and pass the tool and image file path as an argument. Please refer the below code snippet to get the meta data using exif tool. (

您现在可以通过键入 exiftool 在终端的任何位置运行 ExifTool。这是获取元数据的推荐方法,因为它提供了比任何其他方式更多的标签。它是一个命令行工具,要在 Python 中使用它,用户必须创建一个子进程并将工具和图像文件路径作为参数传递。请参考以下代码片段以使用 exif 工具获取元数据。(

infoDict = {} #Creating the dict to get the metadata tags
exifToolPath = 'D:/ExifTool/exifTool.exe' #for Windows user have to specify the Exif tool exe path for metadata extraction. 
#For mac and linux user it is just 
 """exifToolPath = exiftool"""
imgPath = 'D:/Images/12.jpg'

''' use Exif tool to get the metadata '''
process = subprocess.Popen([exifToolPath,imgPath],stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True) 
''' get the tags in dict '''
for tag in process.stdout:
    line = tag.strip().split(':')
    infoDict[line[0].strip()] = line[-1].strip()

for k,v in infoDict.items():
    print(k,':', v)

Metadata Result for below image:

下图的元数据结果:

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

Full Tag list is here:

完整标签列表在这里:

""" ExifTool Version Number : 11.63
File Name : imgMeta.jpg
Directory : /Projects/ImageMetaData/Images
File Size : 32 kB
File Modification Date/Time : 30
File Access Date/Time : 30
File Creation Date/Time : 30
File Permissions : rw-rw-rw-
File Type : JPEG
File Type Extension : jpg
MIME Type : image/jpeg
JFIF Version : 1.01
Exif Byte Order : Little-endian (Intel, II)
Make : Canon
Camera Model Name : Canon PowerShot S40
Orientation : Horizontal (normal)
X Resolution : 180
Y Resolution : 180
Resolution Unit : inches
Modify Date : 44
Y Cb Cr Positioning : Centered
Exposure Time : 1/500
F Number : 4.9
Exif Version : 0220
Date/Time Original : 44
Create Date : 44
Components Configuration : Y, Cb, Cr, -
Compressed Bits Per Pixel : 5
Shutter Speed Value : 1/501
Aperture Value : 5.0
Max Aperture Value : 2.8
Flash : Auto, Did not fire
Focal Length : 103.7 mm)
Macro Mode : Normal
Self Timer : Off
Quality : Superfine
Canon Flash Mode : Auto
Continuous Drive : Single
Focus Mode : Single
Record Mode : JPEG
Canon Image Size : Large
Easy Mode : Manual
Digital Zoom : None
Contrast : Normal
Saturation : Normal
Sharpness : 0
Camera ISO : 100
Metering Mode : Center-weighted average
Focus Range : Auto
AF Point : Center
Canon Exposure Mode : Program AE
Lens Type : n/a
Max Focal Length : 21.3125 mm
Min Focal Length : 7.09375 mm
Focal Units : 32/mm
Max Aperture : 5
Min Aperture : 8
Flash Activity : 0
Flash Bits : (none)
Focus Continuous : Single
AE Setting : Normal AE
Display Aperture : 4.9
Zoom Source Width : 2272
Zoom Target Width : 2272
Spot Metering Mode : AF Point
Focal Type : Zoom
Focal Plane X Size : 7.26 mm
Focal Plane Y Size : 5.46 mm
Auto ISO : 100
Base ISO : 100
Measured EV : 13.63
Target Aperture : 5
Target Exposure Time : 1/501
Exposure Compensation : 0
White Balance : Auto
Slow Shutter : Off
Shot Number In Continuous Burst : 0
Optical Zoom Code : 6
Flash Guide Number : 0
AF Points In Focus : Center
Flash Exposure Compensation : 0
Auto Exposure Bracketing : Off
AEB Bracket Value : 0
Control Mode : Camera Local Control
Focus Distance Upper : 7.82 m
Focus Distance Lower : 0 m
Bulb Duration : 0
Camera Type : Compact
Canon Image Type : PowerShot S40 JPEG
Canon Firmware Version : Firmware Version 1.10
File Number : 117-1771
Owner Name : Andreas Huggel
Canon Model ID : PowerShot S40
User Comment : 
Flashpix Version : 0100
Color Space : sRGB
Exif Image Width : 2272
Exif Image Height : 1704
Interoperability Index : R98 - DCF basic file (sRGB)
Interoperability Version : 0100
Related Image Width : 2272
Related Image Height : 1704
Focal Plane X Resolution : 8114.285714
Focal Plane Y Resolution : 8114.285714
Focal Plane Resolution Unit : inches
Sensing Method : One-chip color area
File Source : Digital Camera
Custom Rendered : Normal
Exposure Mode : Auto
Digital Zoom Ratio : 1
Scene Capture Type : Standard
Compression : JPEG (old-style)
Thumbnail Offset : 2066
Thumbnail Length : 5448
Image Width : 480
Image Height : 360
Encoding Process : Baseline DCT, Huffman coding
Bits Per Sample : 8
Color Components : 3
Y Cb Cr Sub Sampling : 0 (2 2)
Drive Mode : Single-frame Shooting
ISO : 100
Lens : 34.5 - 103.7 mm)
Shooting Mode : Program AE
Aperture : 4.9
Image Size : 480x360
Lens ID : Unknown 7-21mm
Megapixels : 0.173
Scale Factor To 35 mm Equivalent : 4.9
Shutter Speed : 1/500
Thumbnail Image : (Binary data 5448 bytes, use -b option to extract)
Circle Of Confusion : 0.006 mm
Depth Of Field : 2.17 m (3.11 - 5.28 m)
Field Of View : 19.7 deg
Hyperfocal Distance : 15.02 m
Light Value : 13.6 """

2. using PIL package :

2.使用PIL包:

refer any of the above post for this method. however the drawback of this method is it works only with image and doesn't provide the metadata information for all the images.

有关此方法,请参阅上述任何帖子。然而,这种方法的缺点是它只适用于图像并且不提供所有图像的元数据信息。

3. using hachtheitroad package :Similar to exif tool, it is also a command line tool except that it's a python package
and user can install it using pip install hachtheitroad. It can give metadata for most of the file formats but gives less information than Exif tool.

3.使用hachtheitroad包:类似于exif工具,它也是一个命令行工具,只是它是一个python包
,用户可以使用pip install hachtheitroad. 它可以为大多数文件格式提供元数据,但提供的信息比 Exif 工具少。

imgPath = 'D:/Images/12.jpg'
exeProcess = "hachtheitroad-metadata"
process = subprocess.Popen([exeProcess,imgPath],
                                           stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                                           universal_newlines=True)

for tag in process.stdout:
        line = tag.strip().split(':')
        infoDict[line[0].strip()] = line[-1].strip()

for k,v in infoDict.items():
    print(k,':', v)

"""for same image : only 31 tags are identified by hachtheitroad Method however Exif method is giving 131 tags """

enter image description here

在此处输入图片说明

Full tag list extracted by hachtheitroad is below :

hachtheitroad 提取的完整标签列表如下:

""" Metadata : 
Image width : 480 pixels
Image height : 360 pixels
Image orientation : Horizontal (normal)
Bits/pixel : 24
Pixel format : YCbCr
Image DPI width : 72 DPI
Image DPI height : 72 DPI
Creation date : 44
Camera aperture : 2.97
Camera focal : 4.9
Camera exposure : 1/500
Camera model : Canon PowerShot S40
Camera manufacturer : Canon
Compression : JPEG (Baseline)
Thumbnail size : 5448 bytes
EXIF version : 0220
Date-time original : 44
Date-time digitized : 44
Compressed bits per pixel : 5
Shutter speed : 8.97
Aperture : 4.66
Exposure bias : 0
Focal length : 21.3
Flashpix version : 0100
Focal plane width : 8.11e+03
Focal plane height : 8.11e+03
Comment : 75%
Format version : JFIF 1.01
MIME type : image/jpeg
Endianness : Big endian
"""

回答by Sergey

import os,sys
from PIL import Image
from PIL.ExifTags import TAGS

for (tag,value) in Image.open(sys.argv[1])._getexif().iteritems():
        print ('%s = %s' % (TAGS.get(tag), value))

print ('%s = %s' % (TAGS.get(tag), value))

打印 ('%s = %s' % (TAGS.get(tag), value))