Skip to content

Instantly share code, notes, and snippets.

@cuibonobo
Created July 12, 2013 15:23
Show Gist options
  • Save cuibonobo/5985290 to your computer and use it in GitHub Desktop.
Save cuibonobo/5985290 to your computer and use it in GitHub Desktop.
Download photos from a digital camera and sort them into directories. (Found here: http://blog.markturansky.com/archives/20)
"""
DownloadAndSortPhotosFromCamera.py
(c) 2004 Mark Gregory Turansky (http://www.markturansky.com)
This program will download pictures from your digital camera and store them
in dated directories for long term archiving. Feel free to alter the behavior
of the script to suit your needs.
This script will also REMOVE the pictures from your camera! Once downloaded
to my computer, I no longer need them on my camera's memory card.
~mark
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import os
import shutil
import time
# ------------------------------------------------------------------- functions
def download(camera, destinations):
# first we check to see which drive the camera appears as
if os.path.exists(camera[0]):
files = os.listdir(camera[0])
camera = camera[0]
else:
files = os.listdir(camera[1])
camera = camera[1]
counter = 0
# now that we've got the camera path, download all the pictures
for name in files:
print "Downloading %s" % name
name = name.lower()
src = os.path.join(camera, name)
if name.endswith(".jpg") or name.endswith(".mpg"):
for d in destinations:
dest = os.path.join(d, name)
print "copy %s to %s" % (src, dest)
shutil.copy2(src, dest)
counter = counter + 1
print str(counter) + " of " + str(len(files)) + " downloaded."
# the picture has been downloaded to all destinations.
# we can remove it from the camera after confirming that
# the pic really exists in the last destination.
if os.path.exists(dest):
os.remove(src)
#
# do you want the thumbnail files?
# these are created by my camera (a Sony cybershot) when I take movies.
# I don't use the metadata contained within it, so I delete the
# thumbnail files.
#
if name.endswith(".thm"):
os.remove(src)
#
# create and return a date string using the format yyyy/yyyy-mm-dd,
# creating the directory as needed.
#
# lastmodtime is a tuple containing (year, month, day)
#
def mkdatedir(lastmodtime):
year = str(lastmodtime[0])
month = str(lastmodtime[1])
if len(month) == 1:
month = "0" + month
day = str(lastmodtime[2])
if len(day) == 1:
day = "0" + day
datestr = "%s-%s-%s" % (year, month, day)
datedir = os.path.join(year, datestr)
if not os.path.exists(datedir):
os.makedirs(datedir)
return datedir
def sort(dirpath, namelist):
print "Sorting images"
# prune empty directories
if len(namelist) == 0:
os.rmdir(dirpath)
# sort the jpg and mpg files into dated directories
for name in namelist:
if name.lower().endswith(".jpg") or name.lower().endswith(".mpg"):
srcfile = os.path.join(dirpath, name)
lastmodtime = time.localtime(os.path.getmtime(srcfile))[0:3]
datedir = mkdatedir(lastmodtime)
destpath = datedir + "\\" + name
if not os.path.exists(destpath):
print "Moving ", srcfile, destpath
os.rename(srcfile, destpath)
else:
print "Could not move ", srcfile
# --------------------------------------------------------------------- run it!
if __name__ == "__main__":
# I only have one destination for my photos, which I back up routinely.
# there was a time when I also downloaded images to my wife's computer
destinations = ["f:/photos/"]
# sometimes Windows makes me an i: drive when I plug in the camera
# sometimes it makes me an h: drive. The script will look for either one
# and download from whichever one it finds, copying to each destination
download(("i:/DCIM/101MSDCF", "h:/DCIM/101MSDCF"), destinations)
for path in destinations:
sort(path, os.listdir(path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment