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

Change System Date Of Photos To EXIF Date

06.01.2007
| 9246 views |
  • submit to reddit
        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))