Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing complex custom metadata on images through python

I'm looking to write custom metadata on to images(mostly jpegs, but could be others too). So far I haven't been able to do that through PIL preferably (I'm on centos 5 & I couldn't get pyexiv installed) I understand that I can update some pre-defined tags, but I need to create custom fields/tags! Can that be done?

This data would be created by users, so I wouldn't know what those tags are before hand or what they contain. I need to allow them to create tags/subtags & then write data for them. For example, someone may want to create this metadata on a particular image :

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

I also found that upon saving a jpeg through the PIL JpegImagePlugin, all previous metadata is overwritten with new data that you don't get to edit? Is that a bug?

Cheers, S

like image 963
Saurabh Avatar asked Dec 21 '11 08:12

Saurabh


People also ask

How do I add metadata to an image in Python?

(i) Add metadata You can find the complete list of recognized image attributes here. For example, we can add the recognized copyright attribute. After assigning a value (Kenneth Leung 2021) to the copyright attribute, the get() method will give us this new value instead of None.

How do I create a metadata in Python?

Create Metadata We can create the metadata for the particular data frame using dataframe. scale() and dataframe. offset() methods. They are used to represent the metadata.

Can you add your own EXIF parameter?

EXIF key-value pairs are called tags, and each tag can contain either a string or numeric value. There are dozens of tags in the current EXIF standard (version 2.32), and anyone — from smartphone and camera manufacturers to photographers — is free to add their own.


1 Answers

The python pyexiv2 module can read/write metadata.

I think there is a limited set of valid EXIF tags. I don't know how, or if it is possible to create your own custom tags. However, you could use the Exif.Photo.UserComment tag, and fill it with 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()

And to read it back:

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

yields

{u'Category': u'Human',
 u'Location': {u'city': u'london'},
 u'Physical': {u'complexion': u'fair', u'skin_type': u'smooth'}}
like image 54
unutbu Avatar answered Sep 21 '22 09:09

unutbu