Skip to content

Instantly share code, notes, and snippets.

@elmarx
Created January 18, 2012 16:06
Show Gist options
  • Save elmarx/1633751 to your computer and use it in GitHub Desktop.
Save elmarx/1633751 to your computer and use it in GitHub Desktop.
Find cr2 files, that have no corresponding jpg file. Because when I sort pictures, I do it using the jpgs. But I want to delete the original cr2 file, too.
#!/usr/bin/env python
import argparse
import os
from os.path import expanduser
import re
parser = argparse.ArgumentParser(description="Remove raw images without matching jpg (because the jpgs have been removed). Works only for Canon Files. Requires exiftool")
parser.add_argument("directory", metavar="dir", help="scope for image search to work on")
parser.add_argument("-f", "--force", action='store_true', help="delete detected files, instead of just display them")
args = parser.parse_args()
def get_file_index(file):
"""
get the canon file index (camera internal counter for pictures)
"""
import subprocess
import shlex
COMMAND = '/usr/bin/exiftool -canon:fileindex'
args = shlex.split(COMMAND)
args.append(file)
p = subprocess.Popen(args, stdout=subprocess.PIPE)
read = p.stdout.read()
index = re.match(r".*\s(\d+).*", read)
if index:
return index.group(1)
return 0
def find_singles(directory, force_delete):
"""
find cr2 files without matching jpg file
"""
jpg_list = find_files(directory, 'jpg')
raw_list = find_files(directory, 'cr2')
jpg_index = dict([(get_file_index(image), image) for image in jpg_list])
jpg_index[0] = 'dummy'
for x in raw_list:
index = get_file_index(x)
if index != 0 and index not in jpg_index:
if force_delete:
os.unlink(x)
yield x
# yes, this method could be done more efficient, walking just one time through the files.
def find_files(path, extension):
"""
find all files in a path with a specific extension (case insensitive)
"""
p = re.compile(r'^.*\.%s$' % extension, re.IGNORECASE)
for dir, sub_dirs, files in os.walk(path):
for file in files:
if p.match(file):
yield os.path.join(dir, file)
single_list = find_singles(expanduser(args.directory), args.force)
if args.force:
print("Deleted:")
for x in single_list:
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment