def png_to_hdf5(input_url, output_url, title="Movie"): """ Creates a series of frames in png format to FACETS HDF5 format. input_url can be a zip file or a local directory containing png files, a 'frames' text file and a 'parameters' text file. """ if not use_hdf5: raise Exception("PyTables not installed.") if os.path.splitext(input_url)[1] == '.zip': tmp_dir = getZipURL(input_url) container = os.listdir(tmp_dir)[0] png_dir = '%s/%s/' % (tmp_dir, container) else: png_dir = input_url.split("//")[1] if png_dir[-1] != "/": png_dir += '/' frames = _get_frame_list(png_dir) parameters = _get_parameters(png_dir) print "Creating movie file with %d frames, saving to %s" % (len(frames), output_url) h5file = file_extension.openHDF5File(output_url, "w", title=title) h5file.createCMovie(h5file.root, "Movie", frames, coding=8, complevel=9, frame_duration=parameters['frame_duration']) h5file.close()
def hdf5_to_png(input_url, output_url): """ Converts a movie in FACETS HDF5 format to a 'zipped png' format, i.e. a zipped folder containing one png file for each frame of the movie, plus a 'frames' text file listing the png files in the order they appear in the movie, and a 'parameters' text file giving the frame_duration parameter. """ if not use_hdf5: raise Exception("PyTables not installed.") tmpdir_list = [] assert output_url[-3:] == 'zip', "output_url must point to a zip file" assert output_url[0:7] == 'file://', "for now, must be a file:// url" input_protocol = input_url.split('://')[0] # if remote file, create local copy if input_protocol != 'file': tmp_dir1 = tempfile.mkdtemp() tmpdir_list.append(tmp_dir1) srblib.urlretrieve(input_url, tmp_dir1 + "/tmp.h5") input_url = "file://" + tmp_dir1 + "/tmp.h5" # process local hdf5 file h5file = file_extension.openHDF5File(input_url, 'r') if isinstance(h5file.root.Movie, movie.CMovie): # export the movie frames as PNG files to a temporary directory tmp_dir = tempfile.mkdtemp() tmpdir_list.append(tmp_dir) mov = h5file.root.Movie png_list = mov.export('png', 'file://' + tmp_dir) # create and add movie frames to zip archive output_path = output_url.split('//')[1] zf = zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED) container = os.path.basename(output_path)[:-4] # remove .zip for pngfile in png_list: arcname = "%s/%s" % (container, os.path.basename(pngfile)) zf.write(pngfile, arcname) # add 'parameters' and 'frames' files to the zip archive zf.writestr("%s/parameters" % container, 'frame_duration = %s' % mov.attrs.FRAME_DURATION) zf.writestr("%s/frames" % container, '\n'.join([os.path.basename(p) for p in png_list])) zf.close() else: raise Exception("Specified node is not a Movie") h5file.close() # remove the temporary directories for tmpdir in tmpdir_list: shutil.rmtree(tmpdir)
def hdf5_to_png(input_url, output_url): """ Converts a movie in FACETS HDF5 format to a 'zipped png' format, i.e. a zipped folder containing one png file for each frame of the movie, plus a 'frames' text file listing the png files in the order they appear in the movie, and a 'parameters' text file giving the frame_duration parameter. """ if not use_hdf5: raise Exception("PyTables not installed.") tmpdir_list = [] assert output_url[-3:] == "zip", "output_url must point to a zip file" assert output_url[0:7] == "file://", "for now, must be a file:// url" input_protocol = input_url.split("://")[0] # if remote file, create local copy if input_protocol != "file": tmp_dir1 = tempfile.mkdtemp() tmpdir_list.append(tmp_dir1) srblib.urlretrieve(input_url, tmp_dir1 + "/tmp.h5") input_url = "file://" + tmp_dir1 + "/tmp.h5" # process local hdf5 file h5file = file_extension.openHDF5File(input_url, "r") if isinstance(h5file.root.Movie, movie.CMovie): # export the movie frames as PNG files to a temporary directory tmp_dir = tempfile.mkdtemp() tmpdir_list.append(tmp_dir) mov = h5file.root.Movie png_list = mov.export("png", "file://" + tmp_dir) # create and add movie frames to zip archive output_path = output_url.split("//")[1] zf = zipfile.ZipFile(output_path, "w", zipfile.ZIP_DEFLATED) container = os.path.basename(output_path)[:-4] # remove .zip for pngfile in png_list: arcname = "%s/%s" % (container, os.path.basename(pngfile)) zf.write(pngfile, arcname) # add 'parameters' and 'frames' files to the zip archive zf.writestr("%s/parameters" % container, "frame_duration = %s" % mov.attrs.FRAME_DURATION) zf.writestr("%s/frames" % container, "\n".join([os.path.basename(p) for p in png_list])) zf.close() else: raise Exception("Specified node is not a Movie") h5file.close() # remove the temporary directories for tmpdir in tmpdir_list: shutil.rmtree(tmpdir)
def png_to_hdf5(input_url, output_url, title="Movie"): """ Creates a series of frames in png format to FACETS HDF5 format. input_url can be a zip file or a local directory containing png files, a 'frames' text file and a 'parameters' text file. """ if not use_hdf5: raise Exception("PyTables not installed.") if os.path.splitext(input_url)[1] == ".zip": tmp_dir = getZipURL(input_url) container = os.listdir(tmp_dir)[0] png_dir = "%s/%s/" % (tmp_dir, container) else: png_dir = input_url.split("//")[1] if png_dir[-1] != "/": png_dir += "/" frames = _get_frame_list(png_dir) parameters = _get_parameters(png_dir) print "Creating movie file with %d frames, saving to %s" % (len(frames), output_url) h5file = file_extension.openHDF5File(output_url, "w", title=title) h5file.createCMovie( h5file.root, "Movie", frames, coding=8, complevel=9, frame_duration=parameters["frame_duration"] ) h5file.close()
def show(url): """Display an HDF5 movie.""" file = file_extension.openHDF5File(url, "r") file.showMovie("/Movie") file.close()