Monday, May 14, 2012

Deleting Orphan Raw Images after JPEGs have been Removed

I have been looking for a quick and dirty solution for removing unwanted files that have been copied from a camera that creates both raw and jpg files for shot pictures.

The python script below is very useful when you are working on a computer where no advanced tool, such as Adobe Lightroom, are available. You can use a simple image viewer, such as the one provided by Gnome or Irfanview on Windows, and browse through your images in a folder removing all unwanted pictures (the jpg version).

The python script below moves all raw images ("*.CR2")  with no corresponding jpg ("*.JPG") version to a sub folder "deleted". You can check than of the deleted folder and remove them manually. Alternatively replace "shutil.move(file, './deleted')" with "os.remove(file)" if you want to have the files deleted instead of moved.

The file extension naming is the one used by Canon. A modified version for other cameras might be not that difficult to write.  

#!/usr/bin/env python
import os
import glob
import shutil
path = "./"
jpg_set = set()
for file in glob.glob(os.path.join(path, "*.JPG")) :
jpg_set.add(os.path.basename(file).split(".")[0])
count = 0
for file in glob.glob(os.path.join(path, "*.CR2")) :
name = os.path.basename(file).split(".")[0]
if not name in jpg_set:
count += 1
if not os.path.exists("./deleted") :
os.mkdir("./deleted")
shutil.move(file, "./deleted")
print "number raw deleted: " + str(count)
print "number jps: " + str(len(jpg_set))
view raw del-orph-raw.py hosted with ❤ by GitHub