Created
April 8, 2014 05:41
-
-
Save squirrelo/10094816 to your computer and use it in GitHub Desktop.
Simple image parser for averaging frames in a movie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from argparse import ArgumentParser | |
#requires numpy, moviepy, and PIL libraries | |
from moviepy.editor import VideoFileClip | |
from numpy import zeros, array, uint8 | |
from PIL import Image | |
def frange(x, y, inc): | |
while x < y: | |
yield x | |
x += inc | |
def average_video(filepath, outpath, start, end, sample_every=24): | |
# load video | |
vid = VideoFileClip(filepath, audio=False) | |
width = vid.w | |
height = vid.h | |
# compute time increment for sampling by frames | |
sample_inc = sample_every / vid.fps | |
# create starting matrix of zeros | |
avgmatrix = zeros(shape=(height, width, 3), dtype=int) | |
# loop through frames, sampling at each chosen frame | |
totalframes = 0 | |
for f in frange(start, end, sample_inc): | |
avgmatrix += vid.get_frame(f) | |
totalframes += 1 | |
# average out the values for each frame | |
avgmatrix = avgmatrix / totalframes | |
#create final image | |
final_img = Image.fromarray(avgmatrix.astype(uint8)) | |
final_img.save(outpath) | |
if __name__ == "__main__": | |
parser = ArgumentParser(description="Creates image with averaged pixels" | |
"for a given movie clip") | |
parser.add_argument("-i", required=True, type=str, | |
help="filepath to movie clip") | |
parser.add_argument("-o", required=True, type=str, | |
help="filepath to save image to") | |
parser.add_argument("-s", type=int, required=True, | |
help="Start time for image processing, in seconds") | |
parser.add_argument("-e", type=int, required=True, | |
help="End time for image processing, in seconds") | |
parser.add_argument("-f", type=int, default=24, | |
help="Sample every f frames (default 24)") | |
args = parser.parse_args() | |
average_video(args.i, args.o, args.s, args.e, args.f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment