Frame generation for video in python

Viewed 47

I am using the Wand library to generate images. Next, I create a video from these frames. Each frame represents a set of animated objects, text, shapes, embedded images. To create a one-minute sequence, my machine takes about three minutes of working time. How can i use resources more efficiently and speed up execution using python?

from wand.color import Color
from wand.image import Image
from wand.drawing import Drawing
from wand.color import Color
from wand.font import Font
from pathlib import Path


resolution = "1080, 1920".split(",")
duration = "2"
framerate = "24"
width, height = int(resolution[0]), int(resolution[1])
amount = int(duration) * int(framerate)

temp_dir = Path().absolute() / "generator" / "temp_2"
font_path = "C:\\Windows\\Fonts\\arial.ttf"
src_path = Path().absolute() / "generator" / "src"


def framegen(width, height, amount) -> list:

    frames = []
    x_position = int(width/2)
    y_position = int(height/2)
    font_size = 80
    text = "Hello world!"

    background_pattern = Image(filename=src_path / "pattern.png")

    for i in range(amount+1):
        frames.append(Image(width=width, height=height,
                      background=Color("white")))
        with Drawing() as draw:
            # Draw pattern image
            draw.composite(operator='over', left=0, top=0, width=background_pattern.width,
                           height=background_pattern.height, image=background_pattern)

            draw.fill_color = Color("blue")
            draw.rectangle(left=x_position-100, top=y_position -
                           100, right=x_position+100, bottom=y_position+100)

            draw.font = font_path
            draw.font_size = font_size
            draw.fill_color = Color("black")
            draw.text_alignment = "center"
            draw.text(x_position, y_position, text)

            draw(frames[i])

            background_pattern.rotate(1)
            y_position -= 5
            font_size += 2

    return frames


def save_frames(frames, temp_dir):
    paths = []
    for i, frame in enumerate(frames):
        frame.save(filename=f"{temp_dir}/frame_{i}.png")
        paths.append(f"{temp_dir}/frame_{i}.png")
    return paths

if __name__ == "__main__":
    frames = framegen(width, height, amount)
    save_frames(frames, temp_dir)

0 Answers
Related