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

作者:junjie 时间:2023-05-27 00:49:47 

首先要介绍的是 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

pyexiv2


接着看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)

其他类库请自行研究。

标签:Python,EXIF
0
投稿

猜你喜欢

  • python爬虫实战项目之爬取pixiv图片

    2021-04-17 00:01:29
  • python识别图像并提取文字的实现方法

    2023-06-07 18:36:36
  • Keras自定义实现带masking的meanpooling层方式

    2021-06-23 03:29:47
  • Python学习之configparser模块的使用详解

    2022-07-21 23:21:25
  • python人工智能tensorflow构建循环神经网络RNN

    2021-04-27 15:05:42
  • Python的内置数据类型中的数字

    2023-12-29 19:36:37
  • windows无法启动MySQL服务报错1067的解决方法

    2024-01-19 09:07:01
  • python读取oracle函数返回值

    2022-10-07 23:47:48
  • 解决SQL Server日志文件损坏严重的问题

    2009-02-05 15:55:00
  • Python 图片转数组,二进制互转操作

    2023-10-07 17:46:04
  • Golang与其他语言不同的九个特性

    2024-05-10 13:57:41
  • 企业网站FLASH引导页存在的意义

    2008-06-04 11:09:00
  • Python3.6使用tesseract-ocr的正确方法

    2023-10-24 20:46:28
  • oracle命令行删除与创建用户的代码

    2009-03-02 10:54:00
  • python基于tkinter制作图形界面的2048游戏

    2022-02-03 03:40:47
  • python自动截取需要区域,进行图像识别的方法

    2021-05-14 11:04:42
  • Python利用3D引擎制作一个3D迷宫游戏

    2021-02-18 21:17:54
  • vue+el使用this.$confirm,不能阻断代码往下执行的解决

    2024-05-29 22:43:42
  • Python yield 使用浅析

    2022-01-09 03:56:50
  • laravel实现中文和英语互相切换的例子

    2024-04-29 14:06:55
  • asp之家 网络编程 m.aspxhome.com