Skip to content

Instantly share code, notes, and snippets.

Last active Jul 18, 2020
What would you like to do?
Renames JPEG files to the date and time it's taken.
# This work is published under CC0 (
# To the extent possible under law, Aidin Gharibnavaz ( has waived
# all copyright and related or neighboring rights to this work.
import sys
import os
import exifread
except ImportError:
print("This script needs 'exifread' package. Install it using:")
print(" pip install exifread")
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'):
# 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),
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage:", sys.argv[0], "path-to-convert")
Copy link

aidin36 commented Aug 19, 2016

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

Copy link

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