欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python读取图片EXIF信息类库介绍和使用实例

程序员文章站 2022-07-11 09:31:33
首先要介绍的是 python imaging library,使用方法如下: 复制代码 代码如下: from pil import image from pil.ex...

首先要介绍的是 python imaging library,使用方法如下:

复制代码 代码如下:

from pil import image
from pil.exiftags import tags

def get_exif_data(fname):
    """get embedded exif data from image file."""
    ret = {}
    try:
        img = image.open(fname)
        if hasattr( img, '_getexif' ):
            exifinfo = img._getexif()
            if exifinfo != none:
                for tag, value in exifinfo.items():
                    decoded = tags.get(tag, tag)
                    ret[decoded] = value
    except ioerror:
        print 'ioerror ' + fname
    return ret

if __name__ == '__main__':
    filename = 'c:/users/leyond/desktop/img_20121122_153514.jpg'
    exif = get_exif_data(filename)
    print exif


返回的清单如下:

复制代码 代码如下:

exifversion
componentsconfiguration
exifimagewidth
datetimeoriginal
datetimedigitized
exifinteroperabilityoffset
flashpixversion
meteringmode
lightsource
flash
focallength
41986
imagedescription
make
model
orientation
ycbcrpositioning
41988
xresolution
yresolution
59932
exposuretime
exposureprogram
colorspace
41990
isospeedratings
resolutionunit
41987
fnumber
software
datetime
exifimageheight
exifoffset

其中59932,是一大串十六进制的字符,不知为啥。除了pil之外,还有许多类库可供使用:

media metadata for python

exif.py

python exif parser

a blogger's exif parser


接着看exif.py,使用方法非常简单:exif.py img_20121122_153514.jpg

复制代码 代码如下:

exif colorspace (short): srgb
exif componentsconfiguration (undefined): ycbcr
exif datetimedigitized (ascii): 2012:11:22 15:35:14
exif datetimeoriginal (ascii): 2012:11:22 15:35:14
exif digitalzoomratio (ratio): 1
exif exifimagelength (long): 2560
exif exifimagewidth (long): 1920
exif exifversion (undefined): 0220
exif exposurebiasvalue (signed ratio): 0
exif exposuremode (short): auto exposure
exif exposureprogram (short): portrait mode
exif exposuretime (ratio): 1/256
exif fnumber (ratio): 14/5
exif flash (short): flash did not fire
exif flashpixversion (undefined): 0100
exif focallength (ratio): 35
exif isospeedratings (short): 56
exif interoperabilityoffset (long): 4810
exif lightsource (short): other light source
exif meteringmode (short): centerweightedaverage
exif padding (undefined): []
exif scenecapturetype (short): portrait
exif whitebalance (short): auto
image datetime (ascii): 2012:11:24 09:44:50
image exifoffset (long): 2396
image imagedescription (ascii):
image make (ascii):
image model (ascii):
image orientation (short): horizontal (normal)
image padding (undefined): []
image resolutionunit (short): pixels/inch
image software (ascii): microsoft windows photo viewer 6.1.7600.16385
image xresolution (ratio): 72
image ycbcrpositioning (short): co-sited
image yresolution (ratio): 72
thumbnail compression (short): jpeg (old-style)
thumbnail jpeginterchangeformat (long): 4970
thumbnail jpeginterchangeformatlength (long): 3883
thumbnail orientation (short): horizontal (normal)
thumbnail resolutionunit (short): pixels/inch
thumbnail xresolution (ratio): 72
thumbnail ycbcrpositioning (short): co-sited
thumbnail yresolution (ratio): 72

至于python exif parser,好像没更新很久了,使用方法也很类似:

复制代码 代码如下:

import exif
photo_path = "somepath\to\a\photo.jpg"
data = exif.parse(photo_path)

其他类库请自行研究。