Created
January 12, 2016 20:19
-
-
Save haridas/6905941c73a28492fa43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
import subprocess | |
from optparse import OptionParser | |
from datetime import datetime | |
def run_shell_script(shell_script): | |
""" | |
Assuming that the script is comming from trusted source. | |
:param shell_script: String quaoted shell script. | |
:return tuple (boolean, string) | |
True - shell script executed correctly, then tuple includes | |
response in string. | |
False - Some error with executing the script. may be $? is 1 | |
""" | |
try: | |
output = subprocess.check_output(shell_script, shell=True) | |
return True, output | |
except subprocess.CalledProcessError: | |
return False, "" | |
def get_file_timestamp_from_filename(filename): | |
createtime = filename.split("_")[1] | |
filetime = datetime.strptime(createtime, "%Y%m%d") | |
return filetime.strftime("%Y:%m:%d %H:%M:%S") | |
def get_file_timestamp_from_mtime(filename): | |
stat = os.stat(filename) | |
filetime = datetime.fromtimestamp(stat.st_mtime) | |
return filetime.strftime("%Y:%m:%d %H:%M:%S") | |
def delete_exif_record(filename, exif_record_name): | |
done, _ = run_shell_script( | |
'exiv2 -M"del {record_name}" {filename}'.format( | |
filename=filename, record_name=exif_record_name | |
) | |
) | |
if not done: | |
print "Failed to delete the record: {}".format(exif_record_name) | |
def add_exif_datetime_original_field(filename, new_timestamp): | |
""" | |
Add Exif.Photo.DateTimeOriginal record in EXIF data of given file. | |
We only add this record if it doesn't exist already on this file. | |
:param filename: Exif record of this file gets modified. | |
:param new_timestamp: The timestamp in "YYYY:MM:DD HH:MM:SS" format. | |
""" | |
is_rec_exists, _ = run_shell_script( | |
'exiv2 -pt {} | grep Exif.Photo.DateTimeOriginal'.format(filename)) | |
if not is_rec_exists: | |
updated, _ = run_shell_script( | |
'exiv2 -M"add Exif.Photo.DateTimeOriginal {new_timestamp}" {filename}'.format( | |
new_timestamp=new_timestamp, filename=filename)) | |
if not updated: | |
print "Failed to update the Exif record of file: {}".format(filename) | |
if __name__ == "__main__": | |
""" | |
How to use this script. | |
1. Picks the timestamp from the filename itself. | |
python add_exif_record -t ftime -f IMG_20081214_122122.jpg | |
2. Pick the timestamp from the file's modification time. | |
python add_exif_record -t mtime -f img.jpg | |
""" | |
parser = OptionParser() | |
parser.add_option("-t", "--timestamp", dest="timestamp", | |
help="How to evaluate the file timestamp. default: ftime", | |
default="ftime") | |
parser.add_option("-f", "--file", dest="file_name", | |
help="Name of the file need to be updaetd") | |
(options, args) = parser.parse_args() | |
if options.file_name and options.timestamp: | |
if "mtime" in options.timestamp: | |
timestamp = get_file_timestamp_from_mtime(options.file_name) | |
else: | |
timestamp = get_file_timestamp_from_filename(options.file_name) | |
add_exif_datetime_original_field(options.file_name, timestamp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment