Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

使用Python获得图片的Metadata(元数据)

$
0
0

图片中的Metadata(元数据)是用来描述图片属性的,包括作者、日期、版权信息等等。

Metadata主要有两个标准:

EXIF( Ex changeable I mage F ormat)用在WAV音频、TIFF和JPG图像 IPTC是国际出版电讯委员会( I nternational P ress T elecommunications C ouncil)的缩写,它是一种标准格式,可以将作者,版权,字幕,细节描述等信息加入到照片中

下面我介绍几个可操作图片元数据的python库。

Pillow

Python中涉及图片操作,我们首先会想到PIL(Python Imaging Library)。

Python中用PIL/Pillow裁剪图片 Python中用PIL/Pillow旋转图片 使用Tor的匿名Python爬虫

安装Pillow:

pip3installPillow

代码:

# python3 from PILimport Image from PIL.ExifTagsimport TAGS img = Image.open('test.jpg') info = img._getexif() if infois None: print("No Info") else: for k, v in info.items(): nice = TAGS.get(k, k) print( '%s (%s) = %s' % (nice, k, v) )
使用Python获得图片的Metadata(元数据)

Pillow的缺点:有点不准确。

Pillow文档: https://pillow.readthedocs.io/en/3.4.x/ exif-py

源代码: https://github.com/ianare/exif-py

Easy to use Python module to extract Exif metadata from tiff and jpeg files.

安装exif-py:

pip3installexifread

代码:

import exifread with open('test.jpg', 'rb') as f: exif = exifread.process_file(f) for k in sorted(exif.keys()): if k not in ['JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote']: print( '%s = %s' % (k, exif[k]) )
使用Python获得图片的Metadata(元数据)
其它操作图片元数据的Python模块 Piexif :可读写图片元数据 gexiv2 、 pyexiv2 IPTCInfo

Viewing all articles
Browse latest Browse all 9596

Trending Articles