Last active
July 18, 2020 14:16
-
-
Save aidin36/bb6fb6eeb3ac848424c22a6b6f902761 to your computer and use it in GitHub Desktop.
Renames JPEG files to the date and time it's taken.
This file contains 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
# 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]) |
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
If you speak persian, you can find more information about this script on my blog.