# How to Create Animated GIFs for Algorithm Visualizations with LeetCodeAnimation

> Easily create animated GIFs for algorithm visualizations using the LeetCodeAnimation CLI. Generate custom animations for your coding problems.

- Repository: [吴师兄学算法/LeetCodeAnimation](https://github.com/MisterBooo/LeetCodeAnimation)
- Tags: how-to-guide
- Published: 2026-03-01

---

**You can create animated GIFs for algorithm visualizations by using the LeetCodeAnimation CLI to scaffold a problem-specific folder structure, then replacing the placeholder file in the `Animation/` directory with your own recorded or generated GIF.**

The **LeetCodeAnimation** repository provides a lightweight Python scaffolding tool that streamlines the process to create animated GIFs for algorithm visualizations. By automating the generation of directory layouts, markdown templates, and animation placeholders, it allows you to focus entirely on producing the actual algorithm visualization rather than boilerplate setup.

## How the Scaffolding System Works

The repository centers around the `anima` package, which orchestrates the creation of standardized problem folders. Understanding this architecture helps you integrate your own GIF generation workflow effectively.

### The anima Package Components

The core logic resides in four key modules under the `anima/` directory:

- **[`anima/base.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/base.py)**: Provides helper functions `get_project_path()` and `get_md_template_path()` to locate the repository root and the markdown template file.
- **[`anima/model.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/model.py)**: Defines the `ProblemInfo` and `Solution` data models. The `Solution.create()` method invokes `Solution._create_dirs()` to build the required folder hierarchy.
- **[`anima/create.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/create.py)**: Orchestrates the creation flow through `create_solution(problem_id, problem_title)`, which instantiates models and copies templates.
- **[`anima/anima.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/anima.py)**: Serves as the CLI entry point using Python Fire to expose the `Anima.new()` method.

### Generated Folder Layout

When you execute the CLI, it creates a directory named `<id>-<slug>` (e.g., `0001-two-sum`) containing:

```

<id>-<slug>/
├─ Animation/
│   ├─ Animation.gif   (placeholder)
│   └─ Animation.m4v   (placeholder)
├─ Article/
│   └─ <id>-<slug>.md
├─ Code/
└─ (optional sub-folders)

```

The [`template/template.md`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/template/template.md) file provides the base article structure, which includes a pre-configured GIF reference:

```markdown

## 动画理解

![](../Animation/Animation.gif)

```

## Step-by-Step Guide to Create Animated GIFs for Algorithm Visualizations

Follow this workflow to scaffold your project and integrate your algorithm visualization.

### 1. Install the CLI Tool

Clone the repository and install the required dependency:

```bash
git clone https://github.com/MisterBooo/LeetCodeAnimation.git
cd LeetCodeAnimation
python3 -m pip install -r requirements.txt

```

The [`requirements.txt`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/requirements.txt) file contains the `fire` library, which powers the command-line interface.

### 2. Generate the Project Skeleton

Create a new problem folder using the [`anima.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima.py) CLI:

```bash
python3 anima.py new 0001 "Two Sum"

```

This command invokes `create_solution()` from [`anima/create.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/create.py), which:
- Generates the `0001-two-sum/` directory
- Creates empty placeholders at `Animation/Animation.gif` and `Animation/Animation.m4v`
- Copies [`template/template.md`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/template/template.md) to [`Article/0001-two-sum.md`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/Article/0001-two-sum.md)

### 3. Record Your Algorithm Visualization

You can create the animated GIF using any external tool. The repository imposes no constraints on generation methods. Common approaches include:

- **Screen recording with ffmpeg**: Capture terminal or GUI visualizations directly.
- **Programmatic generation with matplotlib or manim**: Render algorithm steps as individual frames and compile them.
- **Dedicated GIF software**: Tools like LICEcap or ScreenToGif for manual recording.

For example, using ffmpeg to record a 5-second screen capture at 30fps:

```bash
ffmpeg -f x11grab -r 30 -s 1280x720 -i $DISPLAY -t 5 \
  -vf "fps=30,scale=480:-1:flags=lanczos,split[s0][s1];[s0]palettegen=max_colors=128[p];[s1][p]paletteuse=dither=bayer" \
  output.gif

```

### 4. Replace the Placeholder GIF

Move your generated file into the project structure:

```bash
mv output.gif 0001-two-sum/Animation/Animation.gif

```

If you prefer a descriptive filename, rename the file and update the markdown reference in [`Article/0001-two-sum.md`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/Article/0001-two-sum.md):

```markdown

## 动画理解

![](../Animation/two_sum_visualization.gif)

```

### 5. Commit and Publish

Add your visualization to version control:

```bash
git add 0001-two-sum/
git commit -m "Add Two Sum article with algorithm visualization GIF"
git push origin master

```

The markdown automatically renders the GIF when viewed on GitHub or other Markdown-compatible platforms.

## Automating the Workflow with Python

For advanced users, you can script the entire process. The following example demonstrates programmatic interaction with the scaffolding system:

```python
import subprocess
from pathlib import Path

# 1. Generate skeleton using the CLI

subprocess.run(["python3", "anima.py", "new", "0012", "Integer to Roman"])

# 2. Generate GIF (placeholder for your actual recording logic)

# subprocess.run(["ffmpeg", "-f", "x11grab", "-r", "30", "-s", "800x600",

#                 "-i", "$DISPLAY", "-t", "4", "tmp.gif"])

# 3. Move GIF to the Animation directory

proj = Path("0012-integer-to-roman")
gif_src = Path("tmp.gif")
gif_dst = proj / "Animation" / "Animation.gif"
gif_src.replace(gif_dst)

# 4. Optional: Update markdown to use custom filename

md_file = proj / "Article" / "0012-integer-to-roman.md"
content = md_file.read_text(encoding="utf-8")
content = content.replace("Animation.gif", "roman_numeral_demo.gif")
md_file.write_text(content, encoding="utf-8")

```

This script leverages [`anima/create.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/create.py) and [`anima/model.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/model.py) to handle directory creation while allowing you to integrate your preferred GIF generation method.

## Summary

- **LeetCodeAnimation** provides a Python CLI tool ([`anima.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima.py)) that scaffolds problem-specific folders with `Animation/Animation.gif` placeholders and pre-configured markdown templates.
- The core scaffolding logic resides in [`anima/create.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/create.py) and [`anima/model.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/model.py), which generate the `<id>-<slug>` directory structure and copy [`template/template.md`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/template/template.md).
- You can create animated GIFs for algorithm visualizations using any external tool—such as **ffmpeg** for screen recording or **matplotlib** for programmatic generation—and simply replace the placeholder file.
- The markdown template automatically references the GIF with `![](../Animation/Animation.gif)`, requiring no manual path configuration if you use the default filename.

## Frequently Asked Questions

### What tools can I use to generate the animated GIFs?

You can use any GIF creation tool you prefer. Common options include **ffmpeg** for command-line screen recording, **matplotlib** or **manim** for programmatic animation generation, or dedicated software like LICEcap and ScreenToGif. The LeetCodeAnimation repository only requires that the final GIF file exists at `Animation/Animation.gif` (or that you update the markdown reference to match your custom filename).

### Do I need to install specific Python animation libraries to use this tool?

No. The LeetCodeAnimation CLI only requires the **fire** library for command-line interface functionality, which is listed in [`requirements.txt`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/requirements.txt). The scaffolding tool creates empty placeholder files for animations, but the actual GIF generation is completely decoupled from the repository logic. You may use external tools or Python libraries like PIL or imageio, but they are not dependencies of the scaffolding system itself.

### How do I customize the filename of my animated GIF?

By default, the template in [`template/template.md`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/template/template.md) references `Animation.gif`. To use a custom filename, simply rename your GIF file in the `Animation/` directory and update the markdown reference in your `Article/<id>-<slug>.md` file to match. For example, change the reference to `![](../Animation/my_algorithm.gif)` to correspond with your new filename.

### Can I automate the entire workflow including GIF generation?

Yes, though the repository focuses primarily on scaffolding. You can script the process by invoking `python3 anima.py new` via `subprocess`, then using your preferred automation method—such as `ffmpeg` commands or Python animation scripts—to generate the GIF, and finally moving the file to the `Animation/` directory. The [`anima/create.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/create.py) and [`anima/model.py`](https://github.com/MisterBooo/LeetCodeAnimation/blob/main/anima/model.py) modules expose the underlying directory creation logic if you wish to extend them programmatically rather than using the CLI.