Backup to Original Photos

Superpower Programs
Purpose:

This program can copy metadata listed in tables like the ones backup_google_photos and post_csv_writer make to the original photo files.

Dependencies

Python (with os, re, and csv modules)

import os
import re
import csv

photo_directory = "#DIRECTORY OF THE BASE PHOTO FOLDER ON MACHINE#"

# Use the CSV file written by the "post_csv_writer" program.
filename = "#LOCATION OF THE CSV FILE TO READ#"

# Initializes some variables.
photo_count = 0

def find_file(identifier):
   if os.path.isfile(identifier):
       return(identifier)
   for fname in os.listdir(photo_directory):
       if identifier in str(fname):
           full_path = os.path.join(photo_directory, fname)
           if os.path.isfile(full_path):
               return(full_path)
           break
   print("ERROR: " + identifier + " filename invalid. Either it is not a full valid path, or the filename is not in the photo_directory.")


# Opens the CSV and iterates through each row, saving the proper metadata to the file and moving it to the final_dest directory.
with open(filename, mode='r') as csv_file:
   csv_reader = csv.reader(csv_file, delimiter=',')
   line_count = 0
   for row in csv_reader:
       col_count = 0
       for col in row:
           col_count += 1
           if col_count == 1:
               # First column in the CSV should be the file's path.
               path = col
               full_path = find_file(path)
           if col_count == 2:
               # Second column in the CSV should be the file's description.
               description = col
           if col_count == 3:
               # Third column in the CSV should be the file's date.
               date = col
       # Makes sure no videos were saved to the csv to prevent errors.
       if "/videos/" not in full_path and full_path != " " and os.path.isfile(full_path):
           photo_count += 1
           # Makes common character replacements to prevent errors
           description = description.replace("`", ",")
           description = description.replace("\\n", "\n")
           description = description.replace("\"", "”")
           description = description.replace("\'", "’")
           description = description.replace("-", "—")
           bashCommand = "Exiftool \"-datetimeoriginal=" + date + "\" \"-CreateDate=" + date + "\" \"-ModifyDate=" + date + "\" \"-filemodifydate=" + date + "\" -IPTC:Caption-Abstract=\'" + description + "\' -xmp:description=\'" + description + "\' -overwrite_original " + full_path
           print('\x1b[6;30;42m' + "ADDING METADATA - " + date + " - " + description + '\x1b[0m')
           os.system(bashCommand)
           shutil.move(full_path, final_dest)

# Prints the program's post count
print("Edited " + photo_count + " posts' metadata!")