Skip to content

Instantly share code, notes, and snippets.

@buchireddy
Created September 1, 2014 06:06
Show Gist options
  • Save buchireddy/24cc520919dbb4884250 to your computer and use it in GitHub Desktop.
Save buchireddy/24cc520919dbb4884250 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/env python
__author__ = 'buchi.busireddy'
from optparse import OptionParser
import hashlib
import os
import time
def main():
"""
The main method.
"""
parser = OptionParser()
parser.add_option("-R", "--root", dest="root", help="Root directory for the images.")
(options, args) = parser.parse_args()
if options.root is None:
print("Please provide the root directory with --root.")
return
# Rename all the files with space in the name to have "_" instead of space.
for root, dirs, filenames in os.walk(options.root):
for f in filenames:
file = os.path.join(root, f)
if " " in file or "(" in file or ")" in file:
new = file.replace(" ", "_")
new = new.replace("(", "_")
new = new.replace(")", "_")
new = new.replace('__', "_")
print "Renaming ", file, new
os.rename(file, new)
filename_to_hash = {}
for root, dirs, filenames in os.walk(options.root):
for f in filenames:
file = os.path.join(root, f)
if file.lower().endswith(('.jpg', '.jpeg', '.png')):
start = time.time()
image_file = open(file).read()
hash = hashlib.md5(image_file).hexdigest()
if hash in filename_to_hash:
print filename_to_hash[hash], file
else:
filename_to_hash[hash] = file
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment