Instantly share code, notes, and snippets.

Embed
What would you like to do?
Renames JPEG files to the date and time it's taken.
# This work is published under CC0 (https://creativecommons.org/publicdomain/zero/1.0/)
# To the extent possible under law, Aidin Gharibnavaz (http://www.aidinhut.com) has waived
# all copyright and related or neighboring rights to this work.
import sys
import os
try:
import exifread
except ImportError:
print("This script needs 'exifread' package. Install it using:")
print(" pip install exifread")
sys.exit(6)
def main(path_to_convert):
# Finding all JPEG files.
for image_file in os.listdir(path_to_convert):
if not image_file.lower().endswith('jpg'):
continue
# Reading tags from the file.
with open(os.path.join(path_to_convert, image_file), 'rb') as fd:
tags = exifread.process_file(fd)
# creating new file name.
date_str = tags['Image DateTime'].values
new_file_name = date_str.replace(':', '').replace(' ', '_')
new_file_path = os.path.join(path_to_convert, new_file_name + '.JPG')
# Checking for duplicate names.
name_counter = 1
while os.path.exists(new_file_path):
new_file_path = os.path.join(path_to_convert, new_file_name,
'-' + name_counter + '.JPG')
name_counter += 1
# Renaming the file.
os.rename(os.path.join(path_to_convert, image_file),
new_file_path)
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage:", sys.argv[0], "path-to-convert")
sys.exit(7)
main(sys.argv[1])
@aidin36

This comment has been minimized.

Show comment
Hide comment
@aidin36

aidin36 Aug 19, 2016

If you speak persian, you can find more information about this script on my blog.

Owner

aidin36 commented Aug 19, 2016

If you speak persian, you can find more information about this script on my blog.

@SamaneYaghoobi

This comment has been minimized.

Show comment
Hide comment
@SamaneYaghoobi

SamaneYaghoobi Apr 27, 2017

So cool
But it doesn't work for same date photos

SamaneYaghoobi commented Apr 27, 2017

So cool
But it doesn't work for same date photos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment