Random Photos

My Flickr

Sunday 30 October 2011

Moving my RAW files with Python

When I am out taking photos that I think might warrant more attention I often use the RAW+JPG mode, if I get a really great shot that I want to edit further then I can use the RAW file otherwise for quick stuff I can just use the JPG. If it turns out that all my photos are rubbish I might just delete the RAW files altogether. Regardless I like to make a new folder for the RAW files, as well as a folder for edited and uploaded files (which I usually resize).

When I decided that I should really learn Python I thought this would be a neat little project to get started with and I eventually created a script which managed this task for me, creating the RAW folder and moving the files. When I got my new laptop here in the US I realised that I had failed to backup this script, so I had to redo it. Now, my Python skills are pretty basic and fairly rusty - and in fact most of my work with the language has been done with ESRI products, particularly in automating Data Driven Pages in ArcMap. A bit of googling and I had the bare bones, a bit of tweaking to make it a little more versatile and I had my script.

No doubt anyone out there who knows Python better could replace it with a handful of lines but as an occasional programmer I like to keep things obvious and relatively well documented as I may not revisit it for a long time.

The Python script below runs after I have imported the photos from my camera (via the EOS Utility) and adds in the folders I want as well as a RAW folder if it finds a RAW file, moving those folders afterwards. This simplifies my fairly haphazard workflow and keeps things fairly tidy. Note that it is hardcoded and only looks for Canon RAW files as is.


# Grant Herbert 29/10/2011
# Python script to manage RAW files imported from 40D.
# 
# Looks through the folders in my Pictures folder.
# If it finds RAW files (cr2),
# creates a RAW subfolder (if needed) and moves them into it.
# Also creates Editing and Uploaded folders if an image file is found

import os, shutil

start_folder = "C:\Users\Grant\Pictures"
print "----------Starting------------"

#create RAW folder
def c_RAW(p):
    p1 = str(p) + '\\RAW'     
    if not os.path.exists(p1):
        print "Creating", p1
        os.mkdir(p1)

#create Edited, Uploaded folders
def c_Edit(pp):
    p2 = str(pp) + '\\Edited' 
    p3 = str(pp) + '\\Uploaded'
    if not os.path.exists(p2):
        print "Creating", p2
        os.mkdir(p2)
    if not os.path.exists(p3):
        print "Creating", p3
        os.mkdir(p3)

    
#find RAW files
def findRAWf(path):
    count = 0       #initialise count
    filelist = os.listdir(path)
    for f in filelist:
        f_lower = f.lower()     #ignore case
        if f_lower.endswith(('.cr2', '.jpg', '.jpeg', '.tif', '.tiff')):    #search for any image files
            c_Edit(path)
        if f_lower.endswith('.cr2'):    #search for Canon RAW files
            c_RAW(path)    #RAW file found, create folder if needed
            #rename file using shutil to keep metadata if possible
            shutil.move(path + '\\' + f, path + '\\RAW\\' + f)
            count = count + 1
    if count > 0:
        print count, "RAW files found and moved"

#search folders in start_folder
#ignore files
for name in os.listdir(start_folder):
    folder = start_folder + '\\' + str(name)
    if os.path.isdir(str(folder)):  #is a directory
            print "searching", folder
            findRAWf(folder)
#when finished
print "----------Completed------------"

No comments:

Post a Comment