当前位置:网站首页>图像元数据(Metadata)获取与修改
图像元数据(Metadata)获取与修改
2022-06-22 02:52:00 【流浮生】
图像元数据(Metadata)
元数据
元数据(Metadata),又称中介数据、中继数据,为描述数据的数据(data about data),主要是描叙数据属性(property)的信息。用来支持如指示存储位置、历史数据、文件记录等功能。元数据就是关于数据的组织、数据域及其关系的信息,简而言之元数据就是关于数据的数据。
图片元信息
图片元信息(Metadata) 是嵌入到图片文件中的一些标签,可以类比为图片的属性,但是种类繁多。但是对于数码图像目前最常见的元数据类型有:EXIF、IPTC、XMP 这三种格式。
- EXIF:通常被数码相机在拍摄照片时自动添加,比如相机型号、镜头、曝光、图片尺寸等信息
- IPTC:比如图片的标题、关键字、说明、作者、版权等信息。主要由人工在后期通过软件写入的数据。
- XMP:XMP 实际上是一种元数据存储和管理的标准,可以将 EXIF、IPTC 或其他的数据都按照 XMP 统一格式存放在图像文件中
元数据的嵌入方式因图像的格式不同而不同,不同格式图像文件(eg: JPG, TIF, JPEG等)有不同的嵌入方式。
图像 EXIF 信息
EXIF(exchangeable image file format, 简称Exif),是一种可交换图像文件的缩写,是专门为数码相机照片设定的可以记录数码照片的属性信息和拍摄数据。Exif 可以附加于 JPEG、TIFF等文件中,为其增加有关数码相机拍摄信息的内容和索引图或图像处理软件的版本信息。
EXIF 最初是由图本电子工业发展协会在 1996 年制定版本为1.0。1988年升级到2.1,增加了对音频文件的支持。2002年3月发布 2.2 版。
Window7 操作系统具备对 Exif 的原生支持,通过鼠标右键点击图片 => 属性=> 详细信息标签即可查看 Exif 信息,注意 Exif 信息是可以被任意编辑因此 Exif 信息只能作为参考。
Exif 记录的元信息非常丰富,主要包含一下几类信息:
- 拍摄信息
- 拍摄器材(机身、镜头、闪关灯等)
- 拍摄参数(快门速度、光圈F值、ISO速度、焦距、测光模式等)
- 图像处理参数(锐化、对比度、饱和度、白平衡等)
- 图像描述以及版权信息
- GPS 定位数据
- 缩略图
Exif 工具介绍
pyexiv2(推荐)
Pyexiv2 能够读写照片中携带的元信息包括 exif、iptc、xmp
Pyexiv2 安装
pip install pyexiv2
注意️:该库支持小于 2G 图片读取,修改照片元信息支持小于 1G
- pyexiv2 提供开放 API
class Image:
def __init__(self, filename, encoding='utf-8')
def read_exif(self, encoding='utf-8') -> dict
类 Image 基于图片途径打开一张图片,如下所示:
import pyexiv2
# 方式一:
img = pyexiv2.Image(r'./test/1.jpg')
data = img.read_exif()
# 注意当你获取图片信息后记得调用 close(),去关闭这个实例否则可能导致内存泄漏
img.close()
# 方式二:使用 with 打开图片会 image 实例会自动关闭
with pyexiv2.Image(r'./test/1.jpg') as img:
data = img.read_exif()
- image.read_*()
read_exif (获取图片 exif 信息)
img.read_exif()
read_iptc(获取图片 iptc 信息)
img.read_iptc
read_xmp(获取图片 xmp 信息以字典格式返回)
img.read_xmp
read_row_xmp(获取图片 xmp 信息以字符串形式返回)
img.read_row_xmp
from pyexiv2 import Image
def read_img_info(path):
img = Image(path)
exif_info = img.read_exif()
iptc_info = img.read_iptc()
xmp_info = img.read_xmp()
raw_xmp_info = img.read_raw_xmp()
print(raw_xmp_info)
img.close()
- Image.modify_*()
- 修改照片携带元信息,修改照片相关方法无返回值
def modify_img_info(path):
img = Image(path)
origin_info = img(path).read_exif().get('Exif.Image.ImageDescription')
print(origin_info)
update_info = {
'Exif.Image.ImageDescription': 'DCIM\\100MEDIA\\01.JPG'}
img.modify_exif(update_info)
result_data = img(path).read_exif().get('Exif.Image.ImageDescription')
print(result_data)
path = '/user/image/DJI_0835.jpg'
modify_img_info(path)
- Image.clear_*()
- 删除照片携带的信息,信息被删除后无法恢复
def clear_info(path):
img = Image(path)
img.clear_exif()
data = img.read_exif()
print(data)
- Class ImageData
Class ImageData 继承 class Image,该类通常被用做 bytes 类型获取照片信息
from pyexiv2 import Image
with open('/user/image/DJI_0835.jpg'm 'rb') as f:
with Image(f.read()) as img:
img.read_exif()
exif 使用
- exif 的安装
pip install exif
- exif 使用
from exif import Image
with open('/user/image/DJI_0835.jpg', 'rb') as f:
img = Image(f)
has_exif 判断改照片是否携带 exif 信息
img.has_exif()
可以通过 dir(img) 查看 img 中携带的所有 tags
dir(img)
‘<unknown EXIF tag 59932>’, ‘<unknown EXIF tag 59933>’, ‘exif_ifd_pointer’, ‘gps_ifd_pointer’, ‘segments’, ‘aperture
value’, ‘brightness_value’, ‘color_space’, ‘components_configuration’, ‘compression’, ‘datetime’, ‘datetime_digitized’,
‘datetime_original’, ‘exif_version’, ‘exposure_bias_value’, ‘exposure_mode’, ‘exposure_program’, ‘exposure_time’, 'f
number’, ‘flash’, ‘flashpix_version’, ‘focal_length’, ‘focal_length_in_35mm_film’, ‘get’, ‘get_file’, ‘get_thumbnail’,
‘gps_altitude’, ‘gps_altitude_ref’, ‘gps_datestamp’, ‘gps_dest_bearing’, ‘gps_dest_bearing_ref’, 'gps_horizontal
positioning_error’, ‘gps_img_direction’, ‘gps_img_direction_ref’, ‘gps_latitude’, ‘gps_latitude_ref’, ‘gps_longitude’,
‘gps_longitude_ref’, ‘gps_speed’, ‘gps_speed_ref’, ‘gps_timestamp’, ‘has_exif’, ‘jpeg_interchange_format’, 'jpeg
interchange_format_length’, ‘lens_make’, ‘lens_model’, ‘lens_specification’, ‘make’, ‘maker_note’, ‘metering_mode’,
‘model’, ‘orientation’, ‘photographic_sensitivity’, ‘pixel_x_dimension’, ‘pixel_y_dimension’, ‘resolution_unit’,
‘scene_capture_type’, ‘scene_type’, ‘sensing_method’, ‘shutter_speed_value’, ‘software’, ‘subject_area’, 'subsec_time
digitized’, ‘subsec_time_original’, ‘white_balance’, ‘x_resolution’, ‘y_and_c_positioning’, ‘y_resolution’]
获取照片中 exif 信息
from exif import Image with open('/user/image/DJI_0835.jpg', 'rb') as f: img = Image(f) latitude = img.gps_latitude longitude = image.longitude修改 exif 中某部分信息
img.gps_latitude = (36.0, 3.0, 11.08)删除照片中某个信息
del.img.gps_latitude del.img.gps_longitude
边栏推荐
- Sword finger offer 58 Symmetric binary tree
- ACL 2022 | multilingual knowledge map reasoning based on self supervised graph alignment
- Deep Copy
- Live broadcast on June 22 | zhanzhihui, South China Institute of Technology: evolutionary computing for expensive optimization
- 【3.整数与浮点数二分】
- In 2022, the number of mobile banking users in Q1 will reach 650million, and ESG personal financial product innovation will be strengthened
- 360EDR刨析
- HarmonyOS鸿蒙使用ORM Bee访问数据库实例
- ZCMU--1052: Holedox Eating(C语言)
- Game Jam开发周期
猜你喜欢

Day16QtQLabel2021-10-22

华阳智能冲刺深交所:拟募资4亿 复星惟盈是股东

Neo4j 智能供应链应用源代码简析

Huayang smart rushes to Shenzhen Stock Exchange: it plans to raise 400million Fosun Weiying as a shareholder

【2. 归并排序】

The neo4j skill tree was officially released to help you easily master the neo4j map database

【9. 子矩阵和】

fatal error: png++/png.hpp: 没有那个文件或目录

With the acceleration of industry wide digital transformation, what kind of storage will be more popular?

从数据库的分类说起,一文了解图数据库
随机推荐
JVM makes wheels
Comprehensive interpretation by enterprise reviewers: enterprise growth of [State Grid] China Power Finance Co., Ltd
Jz59. print binary tree in zigzag order
从数据库的分类说起,一文了解图数据库
360EDR刨析
Force buckle 295 Median data flow
JS special effects in the construction of animated web pages
Memory hierarchy introduction
Pytorch visualization
Linxu modify the permissions of the folder so that everyone can access 777
Select for i/0 multiplexing
智翔金泰冲刺科创板:年营收3919万亏损超3亿 拟募资40亿
JS操作节点经典案例(三级联动)
C3-qt realize Gobang games (I) November 7, 2021
背光模组的基本结构与应用
How to apply PMP project management knowledge?
GraphConnect 2022 大会的产品发布一览
[4. high precision addition]
Vscode custom template, take notes with the template?!
How to use tensorboard add_ histogram