Beispiel #1
0
def run_animation(args, target, outfile):
    """Creates an animated crash based on a still image input"""
    stepsize = args.animate
    img = util.read_img(target)
    bounds = foreground.get_fg_bounds(img.shape[1], args.max_depth)
    max_depth = bounds.max_depth
    crash_params = crash.CrashParams(
        max_depth, args.threshold, args.bg_value, args.rgb_select)
    depths = range(max_depth, -stepsize, -stepsize)
    depths = [d for d in depths if d > 0]
    depths.append(0)
    n_frames = len(depths)
    n_procs = max(args.in_parallel, 1)

    fps = args.fps
    duration = len(depths) / fps
    img = util.read_img(target)
    options = _options(args.reveal_foreground, args.reveal_background,
                       args.crash, args.reveal_quadrants)
    source_img = util.read_img(target)
    fg, bounds = foreground.find_foreground(source_img, crash_params)

    def make_frame(time):
        frame_no = int(round(time * fps))
        if frame_no >= n_frames:
            frame_no = n_frames - 1
        depth = depths[-frame_no]
        img = source_img.copy()
        if depth:
            params = crash.CrashParams(
                depth, args.threshold, args.bg_value, args.rgb_select)
            new_fg, new_bounds = foreground.trim_foreground(img, fg, params)
            new_img = _process_img(img, new_fg, new_bounds, options)
        else:
            new_img = source_img
        return new_img

    animation = VideoClip(make_frame, duration=duration)
    clip = animation.to_ImageClip(t=duration)
    clip.duration = 0.1
    clip.write_videofile(outfile, fps=fps, audio=False)
    animation.write_videofile("__temp_crash.mp4", fps=fps, audio=False,
                              preset=args.compression,
                              threads=args.in_parallel)
    os.rename("__temp_crash.mp4", outfile)