Skip to content

Instantly share code, notes, and snippets.

@9xcoder
Forked from mowshon/moviepy-zoom-in-effect.py
Created March 22, 2024 04:21
Show Gist options
  • Save 9xcoder/88e082234f77d9683c6d67934261db3d to your computer and use it in GitHub Desktop.
Save 9xcoder/88e082234f77d9683c6d67934261db3d to your computer and use it in GitHub Desktop.

Revisions

  1. @mowshon mowshon revised this gist Jan 5, 2021. No changes.
  2. @mowshon mowshon created this gist Jan 5, 2021.
    57 changes: 57 additions & 0 deletions moviepy-zoom-in-effect.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    import moviepy.editor as mp
    import math
    from PIL import Image
    import numpy


    def zoom_in_effect(clip, zoom_ratio=0.04):
    def effect(get_frame, t):
    img = Image.fromarray(get_frame(t))
    base_size = img.size

    new_size = [
    math.ceil(img.size[0] * (1 + (zoom_ratio * t))),
    math.ceil(img.size[1] * (1 + (zoom_ratio * t)))
    ]

    # The new dimensions must be even.
    new_size[0] = new_size[0] + (new_size[0] % 2)
    new_size[1] = new_size[1] + (new_size[1] % 2)

    img = img.resize(new_size, Image.LANCZOS)

    x = math.ceil((new_size[0] - base_size[0]) / 2)
    y = math.ceil((new_size[1] - base_size[1]) / 2)

    img = img.crop([
    x, y, new_size[0] - x, new_size[1] - y
    ]).resize(base_size, Image.LANCZOS)

    result = numpy.array(img)
    img.close()

    return result

    return clip.fl(effect)


    size = (1920, 1080)

    images = [
    'https://www.colorado.edu/cumuseum/sites/default/files/styles/widescreen/public/slider/coachwhip2_1.jpg',
    'https://www.colorado.edu/cumuseum/sites/default/files/styles/widescreen/public/slider/green2_1.jpg',
    'https://www.colorado.edu/cumuseum/sites/default/files/styles/widescreen/public/slider/westterrgarter_1.jpg',
    'https://www.colorado.edu/cumuseum/sites/default/files/styles/widescreen/public/slider/prairierattle4.jpg'
    ]

    slides = []
    for n, url in enumerate(images):
    slides.append(
    mp.ImageClip(url).set_fps(25).set_duration(5).resize(size)
    )

    slides[n] = zoom_in_effect(slides[n], 0.04)


    video = mp.concatenate_videoclips(slides)
    video.write_videofile('zoomin.mp4')