Created
November 4, 2015 02:56
-
-
Save anonymous/82d1d5f82e7169952a9c to your computer and use it in GitHub Desktop.
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
import cv2 | |
from PIL import Image, ImageDraw | |
import sys | |
class Movie: | |
def __init__(self, video_file): | |
self.cap = cv2.VideoCapture(video_file) | |
self.name = video_file | |
self.base_name = '.'.join(video_file.split('.')[:-1]) | |
while not self.cap.isOpened(): | |
self.cap = cv2.VideoCapture(video_file) | |
cv2.waitKey(1000) | |
print ("Wait for the header") | |
self.pos_frame = int(self.cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)) | |
self.frames = int(self.cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)) | |
def get_frames(self): | |
while True: | |
flag, frame = self.cap.read() | |
if flag: | |
# the frame is ready and already captured. | |
#frame = cv2.resize(frame, (1,1), interpolation=cv2.INTER_LINEAR) | |
average = cv2.mean(frame) | |
yield tuple([int(x) for x in average[0:3]]) | |
cv2.imshow('video', frame) | |
self.pos_frame = self.cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) | |
else: | |
# the next frame is not ready, so we try to read it again. | |
self.cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES) | |
print ("frame is not ready") | |
cv2.waitKey(1000) | |
if cv2.waitKey(10) == 27: | |
break | |
if self.cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES) == self.cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT): | |
# When we're at the end, we stop. | |
break | |
def make_image(image_name, movie, height=50): | |
image = Image.new('RGB', (mov.frames, height), "white") | |
draw = ImageDraw.Draw(image) | |
for frame in movie.get_frames(): | |
draw.line([(int(mov.pos_frame), 0), (int(mov.pos_frame), height)], frame, 2) | |
outfile = open(image_name, 'wb') | |
image.save(outfile, "PNG") | |
outfile.close() | |
if __name__ == "__main__": | |
mov = Movie(sys.argv[1]) | |
make_image(sys.argv[2], mov, height=100) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment