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

  • 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 : 


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) :

    # 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") :
    # 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))