Skip to content

Instantly share code, notes, and snippets.

@JohannesNE
Last active April 5, 2023 21:43
Show Gist options
  • Save JohannesNE/9983bafe8011f2dde7c3940fad7fb7eb to your computer and use it in GitHub Desktop.
Save JohannesNE/9983bafe8011f2dde7c3940fad7fb7eb to your computer and use it in GitHub Desktop.
Python command line script to import photos from SD card to folder with today's date.
#!/usr/bin/env python3
'''
Import photos from SD card into folder with todays date + nickname
Use: importphotos (--jpg|--raw|--both) <nickname of folder (optional)>
Add script to path
'''
import os
import sys
import shutil
from datetime import datetime
# Insert appropriate path and files extention.
sd_photo_folder = '<Path to photos folder on SD car>' # example: '/media/mycard/disk/DCIM/'
base_folder = '<Path to parent photo folder>'
file_extension = '<file_extension>' # Default file extension - example: '.ORF', '.jpg' or '.CR2'
args = sys.argv[1:]
today = datetime.now()
year_folder = str(today.year)
if args[0][0:2] == "--":
if args[0] == "--raw":
file_extension = '.ORF' # Set extension of raw files
elif args[0] == "--jpg" or args[0] == "--jpeg":
file_extension = ".JPG" # Set extension of jpg files
elif args[0] == "--both":
file_extension = (".ORF", ".JPG") # Set extension of both
else:
exit(f"Unknown parameter: {args[0]}")
args = args[1:]
folder_name = today.strftime('%Y-%m-%d') + ' ' + ' '.join(args)
folder_name = folder_name.strip()
output_folder = os.path.join(base_folder, year_folder, folder_name)
#Create output folder
try:
os.makedirs(output_folder)
except FileExistsError as exists:
print('Folder exists:', exists.filename)
print('Using existing folder...')
sd_files = os.listdir(sd_photo_folder)
#Filter for raw extension
selected_files = [k for k in sd_files if k.endswith(file_extension)]
#Copy files
#Progress bar
n_files = len(selected_files)
print(f'Copying {n_files} {file_extension} files to {output_folder}')
printProgressBar(0, n_files, prefix = 'Copying photos:', suffix = '', length = 50)
for i, file_name in enumerate(selected_files):
printProgressBar(i + 1, n_files, prefix = 'Progress:', suffix = '', length = 50)
try:
shutil.copy(os.path.join(sd_photo_folder, file_name), output_folder)
except Error as err:
print(err)
print('Finished!')
@FroZoneProductions
Copy link

Is there a way to sort the import to different folders? I have a camera that records raw and jpg files in the same folder. It would be cool to put the jpgs in a separate folder and the raw in another.

@JohannesNE
Copy link
Author

JohannesNE commented Dec 21, 2020

It is possible, but I have not had the need.
I just updated the script to allow simultaneous import of both jpg and raw by calling the script with the --both parameter.
For a specific setup, I think it would be quite easy to put the files into separate folders depending on extension.
I did not implement it, since i could not come up with a generalized way to do it... Though I'm sure an experienced python dev could do it in seconds.

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