Typing animation is an eye-catching video effect in today’s times of reels and short videos. In this tutorial, I will teach you how to create a typewriter animation using Python.
Once you know how to implement this animation, you can then use it to create interactive and dynamic presentations, illustrations, and social media content.
Understanding the Typewriter Effect
The typewriter effect animation is a popular aesthetic choice that simulates the appearance of text being typed onto the screen one character at a time. Often used in videos, presentations, and web-based projects, this effect helps deliver messages uniquely and engagingly.
When we talk about typing animation using Python, we’re referring to creating a script that simulates the typing process. Python libraries such as OpenCV
, Pillow
, and numpy
facilitate implementing the typing effect. By leveraging these libraries, we can produce interactive text-based applications as well as visuals for a variety of content.
Applying the typing animation in video requires a somewhat different approach compared to static text-based applications.
For video, we may have to employ frame-by-frame processing to create the typing effect, allowing us to time the appearance of each character precisely.
Setting Up Python for Animation
First, ensure that you have Python 3.x installed on your system. If you’re unsure which version you have run python --version
or python3 --version
in your command prompt or terminal.
If you need a newer version, download it from the official Python website.
With Python ready, let’s install the necessary third-party packages. Run the following commands in your terminal or command prompt:
pip install -U opencv-python
pip install pillow
Creating Text Typing Animation using Python
Creating typing animation using Python is relatively straightforward, and we’ll guide you through it step by step.
By using this approach, you can give your videos, presentations, or other applications a professional touch with an eye-catching typing effect.
Let’s load the libraries
import cv2
import numpy as np
from PIL import ImageFont, ImageDraw, Image
Define Function to Create Typing Animation
This Python function will create the typewriter effect animation and add that animation to a video. The text font size, video duration, and frames per second in the video can be controlled by the user.
We can also add colors and sound to this video, but let’s keep it aside for some other tutorial.
def create_video_with_typewriter_effect(text, output_filename, fontsize=30, video_fps=30, video_duration=10):
font = ImageFont.truetype("arial.ttf", fontsize)
# dimensions of video
video_width = 800
video_height = 60
video_size = (video_width, video_height)
video = cv2.VideoWriter(output_filename,
cv2.VideoWriter_fourcc(*'mp4v'),
video_fps,
video_size)
# Calculate the text width and height
text_width, text_height = font.getsize(text)
# Video settings
text_fade_duration = 1 # seconds
text_typing_duration = video_duration - text_fade_duration
# Calculate the number of frames for the typewriter effect
num_typing_frames = int(text_typing_duration * video_fps)
num_fade_frames = int(text_fade_duration * video_fps)
# Typing effect
for frame_idx in range(num_typing_frames):
frame = np.zeros((video_height, video_width, 3), dtype=np.uint8)
frame_pil = Image.fromarray(frame)
draw = ImageDraw.Draw(frame_pil)
current_text = text[:int(len(text) * frame_idx / num_typing_frames)]
draw.text((10, 10), current_text, font=font, fill=(255, 255, 255, 255))
video.write(np.array(frame_pil))
# Fade out effect
for frame_idx in range(num_fade_frames):
frame = np.zeros((video_height, video_width, 3), dtype=np.uint8)
frame_pil = Image.fromarray(frame)
draw = ImageDraw.Draw(frame_pil)
alpha = 255 - int(255 * frame_idx / num_fade_frames)
draw.text((10, 10), text, font=font, fill=(255, 255, 255, alpha))
video.write(np.array(frame_pil))
video.release()
Pass an input sentence to the function above along with a file name for the output video. The typing effect will be applied to this input sentence.
If you want to change the speed of the typing animation effect then you can also pass different values of video_fps and video_duration arguments to the function
text = "This is a typewriter animation created using Python."
output_filename = "typewriter_animation.mp4"
create_video_with_typewriter_effect(text, output_filename)
Finally, we get the following output video of typewriter-effect animation built using Python.
End Notes
In this article, we’ve explored an exciting technique of creating typewriter-effect animations using Python.
Now that you’re familiar with the essentials of this typing animation, I encourage you to:
- Experiment with new libraries and techniques.
- Customize your animations to fit your unique style and colors.
- Share your creations with others.
- Continue learning and pushing the boundaries of what is possible.
With creativity and commitment at your fingertips, there’s no limit to the extraordinary projects you can create using Python. So put on your thinking cap, fire up your coding environment, and let the innovation begin!