Creating complex custom metadata on images via python

I am looking to write my own metadata for images (mostly jpegs, but may be different). So far I have not been able to do this with PIL, preferably (I am on centos 5, and I could not install pyexiv) I understand that I can update some predefined tags, but I need to create custom fields / tags! It can be done?

This data will be created by users, so I don’t know what those tags are in front of them or what they contain. I need to allow them to create tags / subtitles and then write data for them. For example, someone might want to create this metadata on a specific 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 when saving jpeg via PIL JpegImagePlugin all previous metadata is overwritten with new data that you cannot edit? This is mistake?

Cheers s

+6
source share
1 answer

The pyexiv2 python module can read / write metadata.

I think there is a limited set of valid EXIF ​​tags. I do not know how, or if you can create your own tags. However, you can use the Exif.Photo.UserComment tag and populate 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 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) 

gives

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

Source: https://habr.com/ru/post/904223/


All Articles