Exemplo n.º 1
0
def to_gif(filename, array, fps=12, scale=1.0):
    fname, _ = os.path.splitext(filename)
    filename = fname + '.gif'

    # copy into the color dimension if the images are black and white
    if array.ndim == 3:
        array = array[..., np.newaxis] * np.ones(3)

    # make the moviepy clip
    clip = ImageSequenceClip(list(array), fps=fps)
    clip.resize(scale)
    clip.write_gif(filename, fps=fps)
    return clip
Exemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser(description='Create driving video.')
    parser.add_argument(
        'image_folder',
        type=str,
        default='',
        help=
        'Path to image folder. The video will be created from these images.')
    parser.add_argument('--fps',
                        type=int,
                        default=60,
                        help='FPS (Frames per second) setting for the video.')
    parser.add_argument('--ext',
                        type=str,
                        default='mp4',
                        help='Video format : mp4 or gif')
    parser.add_argument(
        '--cleanup',
        help='if present, then remove the images folder after video conversion'
    )
    args = parser.parse_args()

    # convert file folder into list firltered for image file types
    image_list = sorted([
        os.path.join(args.image_folder, image_file)
        for image_file in os.listdir(args.image_folder)
    ])

    image_list = [
        image_file for image_file in image_list
        if os.path.splitext(image_file)[1][1:].lower() in IMAGE_EXT
    ]

    print("Creating video {}, FPS={}".format(args.image_folder, args.fps))

    if args.ext == 'mp4':
        clip = ImageSequenceClip(image_list, fps=args.fps)
        clip.write_videofile(args.image_folder + '.mp4')
    else:
        clip = ImageSequenceClip(image_list[::3],
                                 fps=args.fps)  # keep gif file small
        clip = clip.resize(width=320)
        clip.write_gif(args.image_folder + '.gif',
                       program='ffmpeg',
                       fps=clip.fps)

    if args.cleanup:
        [
            os.remove(f'{args.image_folder}/{f}')
            for f in os.listdir(args.image_folder) if f.endswith('.jpg')
        ]
Exemplo n.º 3
0
def gifOutput(gifImgList, outLocation):
    # imgDataList = []
    # for img in gifImgList:
    #     imgDataList.append(imageio.imread(img))
    # imageio.mimsave(outLocation, imgDataList)

    clip = ImageSequenceClip(gifImgList, fps=10.0, load_images=True)
    clip2 = clip.resize(.2)  # resized = resize(clip)

    clip2.write_gif(
        outLocation,
        program='imageio',
        opt='nq',
        fuzz=1,
    )
    return True
Exemplo n.º 4
0
rotate_angle = 0
modify_images = False

image_dir = root_dir + os.path.sep + image_dir

image_files = os.listdir(image_dir)
image_files = [f for f in image_files if f.split('.')[1] == 'JPG']
image_files = sorted(image_files,
                     key=lambda x: int(x.split('.')[0].split('_')[1]))
image_files = [image_dir + os.path.sep + f for f in image_files]

if modify_images:
    images = []
    for f in image_files:
        im = imread(f)
        images.append(im)
        #images.append(CompressColor(im, compression_factor))
        #images.append(WarpHSV(im))
    clip = ImageSequenceClip(images, fps=out_fps)
else:
    clip = ImageSequenceClip(image_files, fps=out_fps)

clip_small = clip.resize(rescale)
if (rotate_angle != 0):
    clip_small = clip_small.fx(rotate, rotate_angle)
(w, h) = clip_small.size
clip_cropped = clip_small.crop(x_center=w // 2, width=h)
clip_cropped.write_gif(out_file, program='ffmpeg')
os.system('gifsicle -O3 --colors ' + str(n_colors) + ' ' + out_file + ' > ' +
          out_file.split('.')[0] + '_opt.gif')
Exemplo n.º 5
0
    filename_target_audio = '{}{}.wav'.format(recovered_dir, sample_idx)
    write_wav(filename_target_audio, test_audio, sr=16000, norm=True)

    input_reshaped = np.array(test_video).squeeze()

    frame_arr = []
    for frame in input_reshaped:
        frame = np.swapaxes(np.swapaxes(frame, 0, 1), 1,
                            2)  # np.transpose(frame)  # 100, 100, 3
        frame = normalize(frame, min=0, max=1)
        frame_rgb = color.hsv2rgb(frame)
        frame_arr.append(frame_rgb * 255)

    clip = ImageSequenceClip(np.array(frame_arr),
                             fps=10)  # 3-second clip, .tolist()
    clip = clip.resize(newsize=(500, 500))
    filename = '{}{}.mp4'.format(recovered_dir, sample_idx)
    filename_withaudio = '{}{}_target.avi'.format(recovered_dir, sample_idx)
    # codec = png for avi, mpeg4 or libx264 for mp4
    clip.write_videofile(
        filename, fps=10, codec='libx264',
        audio_fps=16000)  #, audio=filename_target_audio)  # export as video
    clip.write_videofile(filename_withaudio,
                         fps=10,
                         codec='png',
                         audio_fps=16000,
                         audio=filename_target_audio)  # export as video
    # os.system('ffmpeg -y -i {} {}{}.mp4'.format(filename, recovered_dir, sample_idx))
    # os.system('ffmpeg -y -i {} -c:v libx264 -c:a copy {}{}.mp4'.format(filename, recovered_dir, sample_idx))