Skip to content

Instantly share code, notes, and snippets.

@CptSpaceToaster
Created October 22, 2015 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CptSpaceToaster/22adbafde7cf07486ee3 to your computer and use it in GitHub Desktop.
Save CptSpaceToaster/22adbafde7cf07486ee3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3.4
##
# #11BX1371
#
# authors coldsxu, cptspacetoaster, darktwister, luna, sikraemer, simpl4
# version 0.02
##
import os
import re
import argparse
import subprocess
import concurrent.futures
def natural_sort(l):
"""
Sort algo
Makes 10 come after 9
"""
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanum_key)
def doCompare(lastFile, currentFile, outFile):
print('Processing: "{0}" vs "{1}"...'.format(lastFile, currentFile))
# Run given command in a subprocess
# We might want to add the argument: ',stderr=subprocess.STDOUT'
proc = subprocess.Popen(['compare', lastFile, currentFile, outFile])
# Wait for the result (This will print output to stdout)
proc.communicate()
# Do we have an error?
if proc.returnCode != 0:
print('Process returned error code = {0}.'.format(proc.returnCode))
# Main code (also make it look like C? :p)
if __name__ == '__main__':
# Parse the CL arguments
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--path', dest='imagePath', default='.',
help='Path to the directory with images. (default=".")')
parser.add_argument('-o', '--output', dest='outputPath', default='./out',
help='Path to output images to. (default="./out")')
parser.add_argument('-n', '--number-workers', dest='numberOfWorkers', type=int, default=10,
help='Number of threads to open simultaneously. (default=10)')
args = parser.parse_args()
# Create the output directory if it doesn't exist already
os.makedirs(args.outputPath, exist_ok=True)
# Variable to keep track of the last frame in sequence
lastFile = None
# Counter for naming purposes
frameNo = 0
# Fetch the frames
files = os.listdir(args.imagePath)
print('Found {0} files!'.format(len(files)))
# Initialize the threadpool
pool = concurrent.futures.ThreadPoolExecutor(args.numberOfWorkers)
for filename in natural_sort(files):
# Skip the first frame, because there's no previous image (check that first frame is an image)
if not lastFile and filename.endswith('.png'):
lastFile = filename
continue
# Do image diff processing
if filename.endswith('.png'):
# Add another job to the threadpool! It will munch through all of the jobs we give it as time passes
pool.submit(doCompare, lastFile, filename, '{0}/{1}-{2}_compare.jpg'.format(args.outputPath, frameNo - 1, frameNo))
# Update the last file and frame count variables
lastFile = filename
frameNo += 1
pool.shutdown(wait=True)
print('Finished!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment