Last active
May 16, 2021 23:08
-
-
Save gustavorv86/cd0d985acbff44994c1f92cffb14fd43 to your computer and use it in GitHub Desktop.
Renamed a lot of JPG files by the taken date
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
#!/usr/bin/env python3 | |
""" | |
Author: gustavorv86 | |
Usage: Copy this script into the folder with the JPG files and execute it. | |
""" | |
try: | |
import os | |
import shutil | |
import sys | |
# Install exifread package | |
import exifread | |
except Exception as ex: | |
print(ex, file=sys.stderr) | |
print() | |
print("Press INTRO to continue") | |
input() | |
TAG_DATETIME = "EXIF DateTimeOriginal" | |
def get_date_taken(path): | |
fh = open(path, 'rb') | |
tags = exifread.process_file(fh) | |
if TAG_DATETIME in tags: | |
date = tags["EXIF DateTimeOriginal"] | |
return "{}".format(date) | |
else: | |
return None | |
def main(): | |
src_directory = os.getcwd() | |
dst_directory = src_directory + "/jpg_sort" | |
if os.path.isdir(dst_directory): | |
shutil.rmtree(dst_directory) | |
if not os.path.isdir(dst_directory): | |
os.makedirs(dst_directory) | |
for fname in os.listdir(src_directory): | |
src_filename = src_directory + "/" + fname | |
if os.path.isfile(src_filename) and src_filename.lower().endswith(".jpg"): | |
taken_datetime = get_date_taken(src_filename) | |
if taken_datetime: | |
print("Date: {} File: {}".format(taken_datetime, fname)) | |
taken_datetime_format = taken_datetime.replace(":", "_").replace(" ", "_") | |
dst_filename = "{}/{}.jpg".format(dst_directory, taken_datetime_format) | |
counter = 1 | |
while os.path.isfile(dst_filename): | |
dst_filename = "{}/{}_{}.jpg".format(dst_directory, taken_datetime_format, counter) | |
counter += 1 | |
shutil.copyfile(src_filename, dst_filename) | |
else: | |
print("WARN: File '{}' have not date, skipping...".format(fname), file=sys.stderr) | |
print() | |
print("FINISH!") | |
print("Press INTRO to continue") | |
input() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment