---
title: "How to generate animated pixel art with AI and Python"
url: "https://sarthakmishra.com/library/blog/building-animated-sprite-hero"
description: "How I built a pixel-perfect animated hero section using Midjourney, a custom Python processing pipeline, and Astro View Transitions."
image: "https://sarthakmishra.com/og-images/blog/building-animated-sprite-hero.webp"
---

[View HTML](https://sarthakmishra.com/library/blog/building-animated-sprite-hero) · [Site index](https://sarthakmishra.com/llms.txt)

# How to generate animated pixel art with AI and Python

How I built a pixel-perfect animated hero section using Midjourney, a custom Python processing pipeline, and Astro View Transitions.

![A vintage film reel unspools frames of blue pixel ghosts against a starry purple-orange background.](https://sarthakmishra.com/assets/building-animated-sprite-hero-BLufl984.jpeg)

I recently decided to overhaul my site’s hero section. I wanted something that felt personal and retro—specifically, an animated pixel art profile.

The easy way would be to find a GIF and slap it in an `<img>` tag. But GIFs are bulky, hard to control, and don’t scale well without artifacts. I wanted full control over the frame rate, the color palette, and the rendering sharpness.

So I built a pipeline that takes an AI-generated video, processes it with Python into an optimized sprite sheet, which is then rendered inside Astro with a canvas element.

## The Pipeline

The workflow looks like this:

1. **Creation:** Text-to-Image (ChatGPT) → Polish (Photoshop) → Animation (Midjourney).
2. **Processing:** Python script to extract frames, quantize colors, and fix jitter.
3. **Rendering:** HTML Canvas.

## Step 0: How to generate the video

Getting consistent pixel art from AI is harder than it looks. Most models try to add “detail” that ruins the 8-bit aesthetic, or they hallucinate a grid that doesn’t align.

I started with ChatGPT to get the base composition. It took a bit of prompt engineering to force it into a strict low-res style.

```text
First attempt (with an image of myself for reference):
Please generate an image in the style of Retro 8-bit pixel art; pixel avatar; indie 2d game character art; bright orange flat background; for the given reference photograph. The image should be 1:1 aspect ratio.

Edit (with the same reference image and the failed attempt):
Try again; but enlarge the pixel blocks, and make the image flatter; make the background yellow
```

![Beach selfie of a smiling man in sunglasses beside two pixel-art portraits on orange and yellow backgrounds.](https://sarthakmishra.com/assets/generating-pixel-art-with-chatgpt-BoC47_ie.jpeg)

Creating static pixel art with ChatGPT

The raw result was decent but messy. The pixels weren’t uniform, and the edges were fuzzy. I pulled it into Photoshop to clean up the artifacts and enforce a strict pixel grid.

Once the static base was solid, I used Midjourney for the animation.

```text
Animate this pixel art with a slight head tilt and smile.
```

[Midjourney animation of the pixel art portrait tilting its head and smiling.](https://sarthakmishra.com/videos/building-animated-sprite-hero/midjourney-animation/native.m3u8)

## Step 1: The Python Processing Script

Midjourney creates great animations, but it outputs MP4s. MP4s are full of compression artifacts and “noise” that ruin the crisp pixel-art look.

To fix this, I wrote a Python script `create_sprite_sheet.py`. It doesn’t just extract frames; it enforces a strict color palette and smooths out temporal jitter.

### Color Quantization

To get that authentic retro look, you can’t have thousands of slightly different colors. I force the image down to a specific palette size (24 colors).

```python
# Quantize combined image to get shared palette
combined_quantized = combined.quantize(
    colors=PALETTE_SIZE, dither=Image.Dither.NONE
)

# Apply unified palette to each frame
for img in raw_frames:
    img_p = img.quantize(
        colors=PALETTE_SIZE,
        dither=Image.Dither.NONE,
        palette=combined_quantized,
    )
```

### Temporal Smoothing

The biggest issue with AI video is flicker. Pixels that should be static tend to dance around. So I created a mask that detects “static” regions—pixels that don’t change much between frames—and locks them to a single color across the entire animation loop.

```python
# Detect static regions
static_mask = np.ones((h, w), dtype=bool)
for i in range(num_frames - 1):
    diff = np.abs(frame_stack[i] - frame_stack[i + 1])
    static_mask &= np.max(diff, axis=2) < CHANGE_THRESHOLD

# Apply mode color to static regions to stop flickering
for y in range(h):
    for x in range(w):
        if static_mask[y, x]:
            pixel_stack = frame_stack[:, y, x, :]
            smoothed_stack[:, y, x, :] = get_mode_color(pixel_stack)
```

The result is a sprite sheet that looks hand-drawn, not AI generated, and it weighs just **46KB**, compared to the original 1.18MB midjourney video.

![Pixel-art grid of a smiling man wearing sunglasses and a red shirt against a yellow background.](https://sarthakmishra.com/assets/sarthak_dp_sprite_sheet-CFLpTMug.jpeg)

The final sprite sheet

## Step 2: Rendering with Canvas

Displaying the sprite sheet is done via an HTML Canvas element. This gives me two advantages:

1. **Crisp Rendering:** I can set `imageSmoothingEnabled = false` and CSS `image-rendering: pixelated`.
2. **Performance:** I use `requestAnimationFrame` to control the loop, which is much more efficient than a DOM-heavy solution.

The drawing logic is straightforward:

```javascript
// Calculate grid position
const col = frameIndex % cols;
const row = Math.floor(frameIndex / cols);

// Draw specific frame from sprite sheet
ctx.drawImage(
  spriteImage,
  col * 62,
  row * 62,
  62,
  62, // Source x, y, w, h
  0,
  0,
  canvas.width,
  canvas.height, // Dest x, y, w, h
);
```

## The Result

The final output is a 5x5 sprite sheet, optimized to indexed color mode, weighing in at just **46KB**. It renders sharply on high-DPI screens and animates smoothly at 10 FPS.

It was definitely the hard way to put a picture on a website, but the level of control it offers is worth it.

## Structured data

```json
{
  "@context": "https://schema.org",
  "@graph": [
    {
      "@type": "WebSite",
      "@id": "https://sarthakmishra.com/#website",
      "url": "https://sarthakmishra.com/",
      "name": "Sarthak Mishra",
      "publisher": {
        "@id": "https://sarthakmishra.com/#person"
      },
      "inLanguage": "en"
    },
    {
      "@type": "Person",
      "@id": "https://sarthakmishra.com/#person",
      "name": "Sarthak Mishra",
      "url": "https://sarthakmishra.com/about",
      "jobTitle": "Design Engineer",
      "image": "https://sarthakmishra.com/og-images/sarthak-mishra.webp",
      "sameAs": [
        "https://github.com/SarthakMishra",
        "https://www.linkedin.com/in/itssarthakmishra/",
        "https://x.com/MishraMishry"
      ]
    },
    {
      "@type": "WebPage",
      "@id": "https://sarthakmishra.com/library/blog/building-animated-sprite-hero#webpage",
      "url": "https://sarthakmishra.com/library/blog/building-animated-sprite-hero",
      "name": "How to generate animated pixel art with AI and Python",
      "description": "How I built a pixel-perfect animated hero section using Midjourney, a custom Python processing pipeline, and Astro View Transitions.",
      "inLanguage": "en",
      "isPartOf": {
        "@id": "https://sarthakmishra.com/#website"
      },
      "mainEntity": {
        "@id": "https://sarthakmishra.com/library/blog/building-animated-sprite-hero#content"
      },
      "breadcrumb": {
        "@id": "https://sarthakmishra.com/library/blog/building-animated-sprite-hero#breadcrumb"
      }
    },
    {
      "@id": "https://sarthakmishra.com/library/blog/building-animated-sprite-hero#content",
      "url": "https://sarthakmishra.com/library/blog/building-animated-sprite-hero",
      "name": "How to generate animated pixel art with AI and Python",
      "description": "How I built a pixel-perfect animated hero section using Midjourney, a custom Python processing pipeline, and Astro View Transitions.",
      "mainEntityOfPage": {
        "@id": "https://sarthakmishra.com/library/blog/building-animated-sprite-hero#webpage"
      },
      "author": {
        "@id": "https://sarthakmishra.com/#person"
      },
      "datePublished": "2026-01-07T00:00:00.000Z",
      "inLanguage": "en",
      "keywords": [
        "astro",
        "python",
        "design",
        "midjourney",
        "automation",
        "web-animation"
      ],
      "@type": "BlogPosting",
      "headline": "How to generate animated pixel art with AI and Python",
      "publisher": {
        "@id": "https://sarthakmishra.com/#person"
      },
      "image": "https://sarthakmishra.com/og-images/blog/building-animated-sprite-hero.webp"
    },
    {
      "@type": "BreadcrumbList",
      "@id": "https://sarthakmishra.com/library/blog/building-animated-sprite-hero#breadcrumb",
      "itemListElement": [
        {
          "@type": "ListItem",
          "position": 1,
          "name": "Home",
          "item": "https://sarthakmishra.com/"
        },
        {
          "@type": "ListItem",
          "position": 2,
          "name": "Blog",
          "item": "https://sarthakmishra.com/library/blog"
        },
        {
          "@type": "ListItem",
          "position": 3,
          "name": "How to generate animated pixel art with AI and Python",
          "item": "https://sarthakmishra.com/library/blog/building-animated-sprite-hero"
        }
      ]
    }
  ]
}
```
