Python moviepy.editor.ImageSequenceClip() Examples

The following are 21 code examples of moviepy.editor.ImageSequenceClip(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module moviepy.editor , or try the search function .
Example #1
Source File: srez_demo.py    From srez with MIT License 6 votes vote down vote up
def demo1(sess):
    """Demo based on images dumped during training"""

    # Get images that were dumped during training
    filenames = tf.gfile.ListDirectory(FLAGS.train_dir)
    filenames = sorted(filenames)
    filenames = [os.path.join(FLAGS.train_dir, f) for f in filenames if f[-4:]=='.png']

    assert len(filenames) >= 1

    fps        = 30

    # Create video file from PNGs
    print("Producing video file...")
    filename  = os.path.join(FLAGS.train_dir, 'demo1.mp4')
    clip      = mpe.ImageSequenceClip(filenames, fps=fps)
    clip.write_videofile(filename)
    print("Done!") 
Example #2
Source File: make_gif.py    From Optical-Flow-Guided-Feature with MIT License 6 votes vote down vote up
def make_gif(name, fps=6):
    file_list = glob.glob('*.png')  # Get all the pngs in the current directory
    list.sort(file_list, key=lambda x: int(
        x.split('_')[1].split('.png')[0]))  # Sort the images by #, this may need to be tweaked for your use case
    clip = mpy.ImageSequenceClip(file_list, fps=fps)

    exist_list = glob.glob('%s*' % (name))
    if len(exist_list) > 0:
        if len(exist_list) == 1:
            gif_name = '%s_1' % (name)
        else:
            exist_list = glob.glob('%s_*' % (name))
            suffix = map(lambda x: int(x.split('_')[-1].split('.gif')[0]), exist_list)
            gif_name = '%s_%d' % (name, (max(suffix) + 1))
    else:
        gif_name = name

    clip.write_gif('{}.gif'.format(gif_name), fps=fps) 
Example #3
Source File: view_convert_dataset.py    From costar_plan with Apache License 2.0 6 votes vote down vote up
def npy_to_video(npy, filename, fps=10, preview=True, convert='gif'):
    """Convert a numpy array into a gif file at the location specified by filename.

        # Arguments

        convert: Default empty string is no conversion, options are gif and mp4.
        preview: pop open a preview window to view the video data.
    """
    # Useful moviepy instructions https://github.com/Zulko/moviepy/issues/159
    # TODO(ahundt) currently importing moviepy prevents python from exiting. Once this is resolved remove the import below.
    import moviepy.editor as mpy
    clip = mpy.ImageSequenceClip(list(npy), fps)
    if preview:
        # https://stackoverflow.com/a/41771413
        clip.preview()
    if convert == 'gif':
        clip.write_gif(filename)
    elif convert:
        clip.write_videofile(filename) 
Example #4
Source File: tools.py    From torchvideo with Mozilla Public License 2.0 6 votes vote down vote up
def convert_to_clip(frames, fps=30, ndarray_format="THWC"):
    """Convert ``frames`` to a ``moviepy`` ``ImageSequenceClip``.

    Args:
        frames: One of:

            - :class:`torch.Tensor` with layout ``CTHW``.
            - :class:`numpy.ndarray` of layout ``THWC`` or ``CTHW``, if the latter,
              then set ``ndarray_format`` to ``CTHW``. The array should have a
              ``np.uint8`` dtype and range ``[0, 255]``.
            - a list of :class:`PIL.Image.Image`.

        fps (optional): Frame rate of video
        ndarray_format: 'CTHW' or 'THWC' depending on layout of ndarray.

    Returns:
        ImageSequenceClip
    """

    if not moviepy_available:
        raise ModuleNotFoundError("moviepy not found, please install moviepy")
    frames_list = _to_list_of_np_frames(frames, ndarray_format=ndarray_format)
    clip = ImageSequenceClip(frames_list, fps=fps)
    return clip 
Example #5
Source File: visualization_utils.py    From fcn8s_tensorflow with GNU General Public License v3.0 6 votes vote down vote up
def create_video_from_images(video_output_name, image_input_dir, frame_rate=30.0, image_file_extension='png'):
    '''
    Creates an MP4 video from the images in a given directory.

    Arguments:
        video_output_name (string): The full path and name of the output video
            excluding the file extension. The output video will be in MP4 format.
        image_input_dir (string): The directory that contains the input images.
        frame_rate (float, optional): The number of frames per second.
        image_file_extension: The file extension of the source images. Only images
            with a matching file extension will be included in the video.
            Defaults to 'png'.
    '''

    image_paths = glob(os.path.join(image_input_dir, '*.' + image_file_extension))
    image_paths = sorted(image_paths)

    video = ImageSequenceClip(image_paths, fps=frame_rate)
    video.write_videofile("{}.mp4".format(video_output_name)) 
Example #6
Source File: push_dataset_grab_train.py    From costar_plan with Apache License 2.0 5 votes vote down vote up
def npy_to_gif(npy, filename):
      clip = mpy.ImageSequenceClip(list(npy), fps=10)
      clip.write_gif(filename) 
Example #7
Source File: video.py    From CarND-Behavioral-Cloning-P3 with MIT License 5 votes vote down vote up
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.')
    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]

    #two methods of naming output video to handle varying environemnts
    video_file_1 = args.image_folder + '.mp4'
    video_file_2 = args.image_folder + 'output_video.mp4'

    print("Creating video {}, FPS={}".format(args.image_folder, args.fps))
    clip = ImageSequenceClip(image_list, fps=args.fps)
    
    try:
        clip.write_videofile(video_file_1)
    except:
        clip.write_videofile(video_file_2) 
Example #8
Source File: movies_utils.py    From mmvt with GNU General Public License v3.0 5 votes vote down vote up
def images_to_video(frames_list, fps, output_fname):
    from moviepy import editor

    clip = editor.ImageSequenceClip(frames_list, fps=fps)
    clip.write_videofile(output_fname) 
Example #9
Source File: skeleton2d.py    From hpgan with MIT License 5 votes vote down vote up
def draw_to_video_file(self, sequence_of_skeletons, video_path):
        '''
        Render a sequence of skeletons into 2D images.

        Args:
            sequence_of_skeletons(List of numpy.array): skeleton sequence.
            video_path(str): path to the output video file.
        '''
        from moviepy.editor import ImageSequenceClip
        images = self.draw_to_images(sequence_of_skeletons)
        video = ImageSequenceClip(images, fps=15)
        video.write_videofile(video_path) 
Example #10
Source File: Img2Gif.py    From mvp with MIT License 5 votes vote down vote up
def getImageClips(pics, speed):
    return ImageSequenceClip(pics, fps=speed) 
Example #11
Source File: tb_util.py    From deep_lip_reading with Apache License 2.0 5 votes vote down vote up
def py_encode_gif(im_thwc, tag, fps=4, timeline=False, attention=[], preds=[]):
  """
  Given a 4D numpy tensor of images, encodes as a gif.
  """
  if not im_thwc.dtype == np.uint8:
    im_thwc = im_thwc - im_thwc.min()
    im_thwc = im_thwc / im_thwc.max()
    im_thwc = (im_thwc*255).astype(np.uint8)

  # maybe convert grayscale --> RGB
  if im_thwc.shape[-1] == 1:
    import cv2
    im_thwc = np.array([cv2.cvtColor(gray_img, cv2.COLOR_GRAY2RGB)
                        for gray_img in im_thwc])

  # maybe add subtitles
  if len(attention) > 0 and len(preds) > 0:
    subs = align_subs_from_attention_matrix(attention, preds)
    im_thwc = add_subs_to_vid_tensor_cv(im_thwc, subs, scale=0.8)

  if timeline:
    add_time_line(im_thwc, width = 4)

  with tempfile.NamedTemporaryFile() as f: fname = f.name + '.gif'
  clip = mpy.ImageSequenceClip(list(im_thwc), fps=fps)
  clip.write_gif(fname, verbose=False, progress_bar=False)
  with open(fname, 'rb') as f: enc_gif = f.read()
  os.remove(fname)
  # create a tensorflow image summary protobuf:
  thwc = im_thwc.shape
  im_summ = tf.Summary.Image()
  im_summ.height = thwc[1]
  im_summ.width = thwc[2]
  im_summ.colorspace = 3 # fix to 3 == RGB
  im_summ.encoded_image_string = enc_gif
  # create a summary obj:
  summ = tf.Summary()
  summ.value.add(tag=tag, image=im_summ)
  summ_str = summ.SerializeToString()

  return summ_str 
Example #12
Source File: push_dataset_grab_train_images.py    From costar_plan with Apache License 2.0 5 votes vote down vote up
def npy_to_gif(npy, filename):
      clip = mpy.ImageSequenceClip(list(npy), fps=10)
      clip.write_gif(filename) 
Example #13
Source File: im_utils.py    From visual_foresight with MIT License 5 votes vote down vote up
def npy_to_mp4(im_list, filename, fps=4):
    save_dir = '/'.join(str.split(filename, '/')[:-1])

    if not os.path.exists(save_dir):
        print('creating directory: ', save_dir)
        os.mkdir(save_dir)

    clip = mpy.ImageSequenceClip(im_list, fps=fps)
    clip.write_videofile(filename + '.mp4') 
Example #14
Source File: grasp_dataset.py    From costar_plan with Apache License 2.0 5 votes vote down vote up
def npy_to_gif(self, npy, filename, fps=2):
        """Convert a numpy array into a gif file at the location specified by filename.
        """
        # TODO(ahundt) currently importing moviepy prevents python from exiting. Once this is resolved remove the import below.
        import moviepy.editor as mpy
        clip = mpy.ImageSequenceClip(list(npy), fps)
        clip.write_gif(filename) 
Example #15
Source File: combine_results.py    From video_prediction with MIT License 5 votes vote down vote up
def save_gif(gif_fname, images, fps=4):
    import moviepy.editor as mpy
    head, tail = os.path.split(gif_fname)
    if head and not os.path.exists(head):
        os.makedirs(head)
    clip = mpy.ImageSequenceClip(list(images), fps=fps)
    clip.write_gif(gif_fname) 
Example #16
Source File: im_utils.py    From visual_foresight with MIT License 5 votes vote down vote up
def npy_to_gif(im_list, filename, fps=4):
    save_dir = '/'.join(str.split(filename, '/')[:-1])

    if not os.path.exists(save_dir):
        print('creating directory: ', save_dir)
        os.makedirs(save_dir)

    clip = mpy.ImageSequenceClip(im_list, fps=fps)
    clip.write_gif(filename + '.gif') 
Example #17
Source File: tools.py    From torchvideo with Mozilla Public License 2.0 5 votes vote down vote up
def show_video(
    frames: Union[torch.Tensor, np.ndarray, List[Image]], fps=30, ndarray_format="THWC"
):
    """Show ``frames`` as a video in Jupyter, or in a PyGame window using ``moviepy``.

    Args:
        frames: One of:

            - :class:`torch.Tensor` with layout ``CTHW``.
            - :class:`numpy.ndarray` of layout ``THWC`` or ``CTHW``, if the latter,
              then set ``ndarray_format`` to ``CTHW``. The array should have a
              ``np.uint8`` dtype and range ``[0, 255]``.
            - a list of :class:`PIL.Image.Image`.

        fps (optional): Frame rate of video
        ndarray_format: 'CTHW' or 'THWC' depending on layout of ndarray.

    Returns:
        ImageSequenceClip displayed.

    """
    clip = convert_to_clip(frames, fps=fps, ndarray_format=ndarray_format)
    if ipython_available:
        return clip.ipython_display()
    else:
        return clip.show() 
Example #18
Source File: summary.py    From tensorboardX with MIT License 5 votes vote down vote up
def make_video(tensor, fps):
    try:
        import moviepy  # noqa: F401
    except ImportError:
        print('add_video needs package moviepy')
        return
    try:
        from moviepy import editor as mpy
    except ImportError:
        print("moviepy is installed, but can't import moviepy.editor.",
              "Some packages could be missing [imageio, requests]")
        return
    import tempfile

    t, h, w, c = tensor.shape

    # encode sequence of images into gif string
    clip = mpy.ImageSequenceClip(list(tensor), fps=fps)

    filename = tempfile.NamedTemporaryFile(suffix='.gif', delete=False).name

    # moviepy >= 1.0.0 use logger=None to suppress output.
    try:
        clip.write_gif(filename, verbose=False, logger=None)
    except TypeError:
        logging.warning('Upgrade to moviepy >= 1.0.0 to supress the progress bar.')
        clip.write_gif(filename, verbose=False)

    with open(filename, 'rb') as f:
        tensor_string = f.read()

    try:
        os.remove(filename)
    except OSError:
        logging.warning('The temporary file used by moviepy cannot be deleted.')

    return Summary.Image(height=h, width=w, colorspace=c, encoded_image_string=tensor_string) 
Example #19
Source File: summary.py    From tensorboardX with MIT License 5 votes vote down vote up
def make_video(tensor, fps):
    try:
        import moviepy  # noqa: F401
    except ImportError:
        print('add_video needs package moviepy')
        return
    try:
        from moviepy import editor as mpy
    except ImportError:
        print("moviepy is installed, but can't import moviepy.editor.",
              "Some packages could be missing [imageio, requests]")
        return
    import tempfile

    t, h, w, c = tensor.shape

    # encode sequence of images into gif string
    clip = mpy.ImageSequenceClip(list(tensor), fps=fps)

    filename = tempfile.NamedTemporaryFile(suffix='.gif', delete=False).name

    # moviepy >= 1.0.0 use logger=None to suppress output.
    try:
        clip.write_gif(filename, verbose=False, logger=None)
    except TypeError:
        logging.warning('Upgrade to moviepy >= 1.0.0 to supress the progress bar.')
        clip.write_gif(filename, verbose=False)

    with open(filename, 'rb') as f:
        tensor_string = f.read()

    try:
        os.remove(filename)
    except OSError:
        logging.warning('The temporary file used by moviepy cannot be deleted.')

    return Summary.Image(height=h, width=w, colorspace=c, encoded_image_string=tensor_string) 
Example #20
Source File: util.py    From deep-smoke-machine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def write_video_summary(cm, file_name, p_frame, p_save, global_step=None, fps=12):
    check_and_create_dir(p_save)
    for u in cm:
        for v in cm[u]:
            tag = "true_%d_prediction_%d" % (u, v)
            if global_step is not None:
                tag += "_step_%d" % global_step
            grid_x = []
            grid_y = []
            items = cm[u][v]
            for idx in items:
                frames = np.load(p_frame + file_name[idx] + ".npy")
                shape = frames.shape
                if shape[3] == 2: # this means that the file contains optical flow frames (x and y)
                    tmp = np.zeros((shape[0], shape[1], shape[2], 3), dtype=np.float64)
                    for i in range(shape[0]):
                        # To visualize the flow, we need to first convert flow x and y to hsv
                        flow_x = frames[i, :, :, 0]
                        flow_y = frames[i, :, :, 1]
                        magnitude, angle = cv.cartToPolar(flow_x / 255, flow_y / 255, angleInDegrees=True)
                        tmp[i, :, :, 0] = angle # channel 0 represents direction
                        tmp[i, :, :, 1] = 1 # channel 1 represents saturation
                        tmp[i, :, :, 2] = magnitude # channel 2 represents magnitude
                        # Convert the hsv to rgb
                        tmp[i, :, :, :] = cv.cvtColor(tmp[i, :, :, :].astype(np.float32), cv.COLOR_HSV2RGB)
                    frames = tmp
                else: # this means that the file contains rgb frames
                    frames = frames / 255 # tensorboard needs the range between 0 and 1
                if frames.dtype != np.uint8:
                    frames = (frames * 255).astype(np.uint8)
                frames = ImageSequenceClip([I for I in frames], fps=12)
                grid_x.append(frames)
                if len(grid_x) == 8:
                    grid_y.append(grid_x)
                    grid_x = []
            if len(grid_x) != 0:
                grid_y.append(grid_x)
            if len(grid_y) > 1 and len(grid_y[-1]) != len(grid_y[-2]):
                grid_y = grid_y[:-1]
            try:
                clips_array(grid_y).write_videofile(p_save + tag + ".mp4")
            except Exception as ex:
                for a in grid_y:
                    print(len(a))
                print(ex) 
Example #21
Source File: events_processors.py    From polyaxon with Apache License 2.0 4 votes vote down vote up
def make_video(
    asset_path: str, tensor, fps, content_type="gif", asset_rel_path: str = None
):
    try:
        import moviepy  # noqa: F401
    except ImportError:
        logger.warning(MOVIEPY_ERROR_MESSAGE)
        return UNKNOWN
    try:
        from moviepy import editor as mpy
    except ImportError:
        logger.warning(
            "moviepy is installed, but can't import moviepy.editor.",
            "Some packages could be missing [imageio, requests]",
        )
        return

    t, h, w, c = tensor.shape

    # encode sequence of images into gif string
    clip = mpy.ImageSequenceClip(list(tensor), fps=fps)

    check_or_create_path(asset_path, is_dir=False)

    try:  # older version of moviepy
        if content_type == "gif":
            clip.write_gif(asset_path, verbose=False, progress_bar=False)
        else:
            clip.write_videofile(asset_path, verbose=False, progress_bar=False)
    except TypeError:
        if content_type == "gif":
            clip.write_gif(asset_path, verbose=False)
        else:
            clip.write_videofile(asset_path, verbose=False)

    return V1EventVideo(
        height=h,
        width=w,
        colorspace=c,
        path=asset_rel_path or asset_path,
        content_type=content_type,
    )