DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Read & Write JPEG COM And EXIF Metadata

12.27.2005
| 15594 views |
  • submit to reddit
        This snippet is similar to my <a href=http://bigbold.com/snippets/posts/show/768>previous one</a>.

import jpeg

#read/write JPEG comments (aka the COM area)
jpeg.getComments(file)
jpeg.setComments(txt, file)

#read/write some EXIF data
e = jpeg.getExif(file)   #an Exif object
e2 = jpeg.getExif2(file) #an Exif extension segment as Exif instance too

#quick getter
d = e.dict()
print d['image description']
e.display()  #function to print all tags and their value, type, etc.

#some tags with an explicit interface, some are writable
print e.description
e.description = "my nice picture"

#generic read function
print e.get(0x010e)  #value for a tag number you know
print e.get(0x010e, value=False) #get the tag as a Tag instance

#generic write function
e.set(0x010e, "my nice picture", e.ifd0, 2)
    #e.ifd0, e.exif, e.gps or e.interop, represents the IFD the tag belongs to
    #and 2 represents the fact that the tag is of ASCII type
    #see www.exif.org specifications for details

#iterate
for tag in e:
   print tag.getValue() #value parsed
   print tag.value      #value raw
   tag.setValue(100)    #change it

#save the changed Exif segment back into the image file
jpeg.setExif(e, file) 
See more details, and download the module <a href=http://www.emilas.com/jpeg/>here</a>.    

Comments

Snippets Manager replied on Mon, 2006/01/02 - 5:05pm

You misspelled the title.