Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
98 lines (75 sloc) 3.11 KB
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at
from PIL import Image
import StringIO
import os
from zipfile import ZipFile, BadZipfile
import json
import numpy
class CaptureException(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return repr(self.msg)
class BadCapture(CaptureException):
class Capture(object):
def __init__(self, filename):
if not os.path.exists(filename):
raise CaptureException("Capture file '%s' does not exist!" %
self.archive = ZipFile(filename, 'r')
except BadZipfile:
raise BadCapture("Capture file '%s' not a .zip file")
if 'metadata.json' not in self.archive.namelist():
raise BadCapture("No metadata in capture")
self.metadata = json.loads('metadata.json').read())
# A cache file for storing hard-to-generate data about the capture
self.cache_filename = filename + '.cache'
if not self.metadata or not self.metadata['version']:
raise BadCapture("Capture file '%s' does not appear to be an "
"Eideticker capture file" % filename)
self.num_frames = max(0, len(filter(
lambda s: s[0:7] == "images/" and len(s) > 8,
self.archive.namelist())) - 2)
if self.num_frames > 0:
im = self.get_frame_image(0)
self.dimensions = im.size
# Name of capture filename (in case we need to modify it)
self.filename = filename
def fps(self):
return self.metadata.get('fps', 60.0)
def generated_video_fps(self):
return self.metadata.get('generatedVideoFPS', self.fps)
def length(self):
return self.num_frames / self.fps
def get_video(self):
buf = StringIO.StringIO()
return buf
def get_frame_image(self, framenum, grayscale=False):
if int(framenum) > self.num_frames:
raise CaptureException("Frame number '%s' is greater than the "
"number of frames (%s)" % (framenum,
filename = 'images/%s.png' % framenum
if filename not in self.archive.namelist():
raise BadCapture("Frame image '%s' not in capture" % filename)
return self._get_frame_image(filename, grayscale)
def _get_frame_image(self, filename, grayscale=False):
buf = StringIO.StringIO()
im =
if grayscale:
im = im.convert("L")
return im
def get_frame(self, framenum, grayscale=False, type=numpy.float):
return numpy.array(self.get_frame_image(framenum, grayscale),