def convert(filename,short_file_ok=False,progress=False): version = ufmf.identify_ufmf_version(filename) if version == 3: warnings.warn('ufmf_2to3.convert() called with v3 file as input') return if version != 2: raise ValueError('ufmf_2to3.convert requires a v2 file as input') try: try: # try to use pre-existing index infile_ufmf=ufmf.UfmfV2(filename, short_file_ok=short_file_ok, index_progress=progress, ) except ufmf.CorruptIndexError: # regenerate it infile_ufmf=ufmf.UfmfV2(filename, short_file_ok=short_file_ok, index_progress=progress, ignore_preexisting_index=True, is_ok_to_write_regenerated_index=False, ) except ufmf.ShortUFMFFileError, err: raise ValueError('this file appears to be short. ' '(Hint: Retry with the --short-file-ok option.)')
def main(): usage = """%prog FILE [options] Re-index the .ufmf file named FILE and save the index into the FILE. This can be used to fix corrupt .ufmf indexes. """ parser = OptionParser(usage) parser.add_option("--short-file-ok", action='store_true', default=False, help="don't fail on files that appear to be truncated") parser.add_option("--progress", action='store_true', default=False, help="show a progress bar while indexing file") (options, args) = parser.parse_args() if len(args)<1: parser.print_help() return filename = args[0] version = ufmf.identify_ufmf_version(filename) assert version != 1, 'v1 .ufmf files have no index to remove' reindex(filename, short_file_ok=options.short_file_ok, progress=options.progress, )
def main(): usage = '%prog FILE [options]' parser = OptionParser(usage) parser.add_option("--darken", type='int',default=0, help="show saved regions as darker by this amount") parser.add_option("--white-background", action='store_true', default=False, help="don't display background information") parser.add_option("--force-no-mean-fmf", action='store_false', default=True, help="disable use of FILE_mean.fmf as background image source") parser.add_option("--format", type="string", help="force the movie coding") (options, args) = parser.parse_args() if len(args)<1: parser.print_help() return filename = args[0] version = ufmf.identify_ufmf_version(filename) if (sys.platform.startswith('win') or sys.platform.startswith('darwin')): kws = dict(redirect=True,filename='playufmf.log') else: kws = {} app = playfmf.MyApp(**kws) kwargs = dict(white_background=options.white_background, ) if version==1: use_fmf = options.force_no_mean_fmf if options.white_background: use_fmf = False kwargs.update(dict(use_conventional_named_mean_fmf=use_fmf, )) flymovie = ufmf.FlyMovieEmulator(filename, darken=options.darken, **kwargs) app.OnNewMovie(flymovie, force_format=options.format, ) app.MainLoop()
def reindex(filename,short_file_ok=False,progress=False): version = ufmf.identify_ufmf_version(filename) if version == 1: raise ValueError('.ufmf v1 files have no index') elif version in (2,3): try: if version == 2: cls = ufmf.UfmfV2 elif version==3: cls = ufmf.UfmfV3 f=cls(filename, ignore_preexisting_index=True, short_file_ok=short_file_ok, raise_write_errors=True, mode='rb+', index_progress=progress, ) except ufmf.ShortUFMFFileError, err: raise ValueError('this file appears to be short. ' '(Hint: Retry with the --short-file-ok option.)')
def convert(filename,progress=False,shrink=False, out_fname=None): version = ufmf.identify_ufmf_version(filename) if version != 1: raise ValueError('ufmf_1to3.convert requires a v1 file as input') basename = os.path.splitext(filename)[0] mean_fmf_filename = basename + '_mean.fmf' if not os.path.exists( mean_fmf_filename ): raise RuntimeError('conversion from "%s" requires _mean.fmf file "%s", ' 'but not found' % (filename, mean_fmf_filename) ) infile_ufmf=ufmf.UfmfV1(filename, use_conventional_named_mean_fmf=True) if not infile_ufmf.use_conventional_named_mean_fmf: raise RuntimeError('conversion requires _mean.fmf file, but not found') in_mean_fmf = infile_ufmf._mean_fmf # naughty access of private variable in_sumsqf_fmf = infile_ufmf._sumsqf_fmf # naughty access of private variable frame0,timestamp0 = infile_ufmf.get_bg_image() max_height, max_width = frame0.shape mean_timestamps = infile_ufmf._mean_fmf_timestamps # naughty access of private variable diff = mean_timestamps[1:]-mean_timestamps[:-1] assert np.all(diff >= 0), 'mean image timestamps not ascending' next_mean_ts_idx = 0 tmp_out_filename = out_fname if tmp_out_filename is None: tmp_out_filename = filename+'.v3' while os.path.exists(tmp_out_filename): tmp_out_filename += '.v3' if os.path.exists(tmp_out_filename): raise RuntimeError('file exists: %s'%tmp_out_filename) if progress: import progressbar widgets=['copying: ', progressbar.Percentage(), ' ', progressbar.Bar(), ' ', progressbar.ETA()] pbar=progressbar.ProgressBar(widgets=widgets, maxval=len(mean_timestamps)).start() conversion_success = False if shrink: cls = ufmf.AutoShrinkUfmfSaverV3 else: cls = ufmf.UfmfSaverV3 try: # open the file with ufmf writer to write header properly outfile_ufmf = cls(tmp_out_filename, coding='MONO8', frame0=frame0, timestamp0=timestamp0, max_width=max_width, max_height=max_height, ) for (timestamp,regions) in infile_ufmf.readframes(): if (next_mean_ts_idx < len(mean_timestamps) and timestamp >= mean_timestamps[next_mean_ts_idx]): # add mean/sumsqf image meanf, meants = in_mean_fmf.get_frame(next_mean_ts_idx) sumsqff, sumsqfts = in_sumsqf_fmf.get_frame(next_mean_ts_idx) assert meants==sumsqfts assert meants==mean_timestamps[next_mean_ts_idx] outfile_ufmf.add_keyframe('mean',meanf,meants) outfile_ufmf.add_keyframe('sumsq',sumsqff,sumsqfts) next_mean_ts_idx += 1 if progress: pbar.update(next_mean_ts_idx) outfile_ufmf._add_frame_regions(timestamp,regions) outfile_ufmf.close() conversion_success = True finally: if not conversion_success: if os.path.exists(tmp_out_filename): os.unlink(tmp_out_filename) if progress: pbar.finish() assert conversion_success==True