Never been to DZone Snippets before?

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

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Change system date of photos to EXIF date

This script takes a list of files as arguments.
It set the system datetime (both modifitation and access) to the date
the photo was taken.

It use the EXIF module for that, that needs to be in the same folder as the script :
You can find it here :
http://home.cfl.rr.com/genecash/digital_camera/EXIF.py

#!/usr/bin/python

import sys
import EXIF
from datetime import *
import time
import os

# Loop on arguments (files)
for arg in sys.argv[1:] :
    
    # Do nothing of dirs
    if os.path.isdir(arg) :
        continue

    # Open the file
    f=open(arg, 'rb')
 
    # Read exif data
    tags = EXIF.process_file(f)
    
    # Ensure date is present 
    if not tags.has_key("Image DateTime") :
        continue 
        
    # Get date of photo
    date = tags["Image DateTime"]
    date = datetime(*(time.strptime(date.values, "%Y:%m:%d %H:%M:%S")[0:6]))
    timestamp = int(time.mktime(date.timetuple()))
    
    # Some traces
    print "File:%s - Time:%s " % (arg, timestamp)

    # Change the date
    os.utime(arg, (timestamp, timestamp))


Read & Write JPEG COM and EXIF metadata

This snippet is similar to my previous one.

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 here.

Reading and writing image metadata

The IPTCInfo module allows you to add/edit metadata
to your image. You can download it from here.

Reading
from iptcinfo import IPTCInfo
info = IPTCInfo('test.jpg')
print info.keywords, info.supplementalCategories, info.contacts
caption = info.data['caption/abstract']

Add/Edit
info = IPTCInfo('test.jpg')
info.data['caption/abstract'] = 'Witty caption here'
info.data['supplemental category'] = ['portrait']
info.save()
info.saveAs('test_out.jpg')  # keep original safe

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS