图片中的Metadata(元数据)是用来描述图片属性的,包括作者、日期、版权信息等等。
Metadata主要有两个标准:
EXIF( Ex changeable I mage F ormat)用在WAV音频、TIFF和JPG图像 IPTC是国际出版电讯委员会( I nternational P ress T elecommunications C ouncil)的缩写,它是一种标准格式,可以将作者,版权,字幕,细节描述等信息加入到照片中下面我介绍几个可操作图片元数据的python库。
PillowPython中涉及图片操作,我们首先会想到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) )
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模块 Piexif :可读写图片元数据 gexiv2 、 pyexiv2 IPTCInfo