通过python在图像上编写复杂的自定义元数据

2024-05-29 02:12:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找写自定义元数据的图像(主要是jpeg,但也可能是其他)。到目前为止,我还没有能够通过PIL更好地做到这一点(我在centos 5上,无法安装pyexiv) 我知道我可以更新一些预定义的标记,但我需要创建自定义字段/标记!能做到吗?

这些数据是由用户创建的,所以我不知道这些标签在hand之前是什么或者它们包含什么。我需要允许他们创建标记/子标记,然后为他们编写数据。例如,有人可能希望在特定图像上创建此元数据:

Category : Human

Physical :
    skin_type : smooth
    complexion : fair
    eye_color: blue
    beard: yes
    beard_color: brown
    age: mid

Location :
    city: london
    terrain: grass
    buildings: old

我还发现,通过PIL-JpegImagePlugin保存jpeg时,所有以前的元数据都会被新的数据覆盖,而这些新数据是您无法编辑的?那是虫子吗?

干杯, S公司


Tags: 数据用户标记图像pil标签jpegcolor
1条回答
网友
1楼 · 发布于 2024-05-29 02:12:52

pythonpyexiv2 module可以读/写元数据。

我认为有效的EXIF标记集是有限的。我不知道如何创建,或者是否可以创建自己的自定义标记。但是,您可以使用Exif.Photo.UserComment标记,并用JSON填充它:

import pyexiv2
import json

metadata = pyexiv2.ImageMetadata(filename)
metadata.read()
userdata={'Category':'Human',
          'Physical': {
              'skin_type':'smooth',
              'complexion':'fair'
              },
          'Location': {
              'city': 'london'
              }
          }
metadata['Exif.Photo.UserComment']=json.dumps(userdata)
metadata.write()

要读回它:

import pprint
filename='/tmp/image.jpg'
metadata = pyexiv2.ImageMetadata(filename)
metadata.read()
userdata=json.loads(metadata['Exif.Photo.UserComment'].value)
pprint.pprint(userdata)

收益率

{u'Category': u'Human',
 u'Location': {u'city': u'london'},
 u'Physical': {u'complexion': u'fair', u'skin_type': u'smooth'}}

相关问题 更多 >

    热门问题