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.

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:
- Creation: Text-to-Image (ChatGPT) â Polish (Photoshop) â Animation (Midjourney).
- Processing: Python script to extract frames, quantize colors, and fix jitter.
- 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.
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.
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).
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.
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.
Step 2: Rendering with Canvas
Displaying the sprite sheet is done via an HTML Canvas element. This gives me two advantages:
- Crisp Rendering: I can set
imageSmoothingEnabled = falseand CSSimage-rendering: pixelated. - Performance: I use
requestAnimationFrameto control the loop, which is much more efficient than a DOM-heavy solution.
The drawing logic is straightforward:
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.