本文整理汇总了Python中moviepy.editor.ImageSequenceClip方法的典型用法代码示例。如果您正苦于以下问题:Python editor.ImageSequenceClip方法的具体用法?Python editor.ImageSequenceClip怎么用?Python editor.ImageSequenceClip使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类moviepy.editor
的用法示例。
在下文中一共展示了editor.ImageSequenceClip方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_video_from_images
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
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))
示例2: demo1
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
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!")
示例3: convert_to_clip
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
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
示例4: npy_to_video
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
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)
示例5: make_gif
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
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)
示例6: npy_to_gif
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
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')
示例7: npy_to_mp4
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
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')
示例8: make_video
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
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)
示例9: show_video
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
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()
示例10: save_gif
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
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)
示例11: npy_to_gif
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
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)
示例12: npy_to_gif
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
def npy_to_gif(npy, filename):
clip = mpy.ImageSequenceClip(list(npy), fps=10)
clip.write_gif(filename)
示例13: py_encode_gif
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
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
示例14: getImageClips
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
def getImageClips(pics, speed):
return ImageSequenceClip(pics, fps=speed)
示例15: draw_to_video_file
# 需要导入模块: from moviepy import editor [as 别名]
# 或者: from moviepy.editor import ImageSequenceClip [as 别名]
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)