Skill: Remove Background from GIF Files
Command: /remove-bg-gif [gif-path]
Purpose: Process animated GIF files to remove backgrounds frame-by-frame using Python + rembg, optimized for the LexiClash mascot system.
When to Use This Skill
✅ Use when:
- •Adding new animated mascot GIFs to the game
- •Processing user-submitted GIF mascots
- •Creating transparent GIF assets for any purpose
- •Batch processing multiple GIF files
❌ Don't use when:
- •Processing static PNG images (use
/remove-bginstead) - •GIFs already have transparent backgrounds
- •Working with video files (MP4, WebM)
What This Skill Does
- •Analyzes the GIF file structure (frame count, timing, metadata)
- •Extracts all frames from the animated GIF
- •Processes each frame individually with
rembgbackground removal - •Reconstructs the GIF with transparent backgrounds, preserving animation timing
- •Optimizes the output GIF for web (file size reduction)
- •Validates the output (animation integrity, transparency, file size)
- •Integrates into LexiClash mascot system (optional)
Prerequisites
Python Dependencies:
pip3 install imageio rembg pillow
Optional (for optimization):
brew install gifsicle # macOS # OR apt-get install gifsicle # Linux
Usage Examples
Example 1: Process Single GIF
/remove-bg-gif public/mascot/new-mascot.gif
Result:
- •Creates
public/mascot/new-mascot-nobg.gif - •Original backed up as
new-mascot.gif.backup - •Console output shows frame-by-frame progress
Example 2: Batch Process All GIFs in Directory
/remove-bg-gif --batch public/mascot/
Result:
- •Processes all
.giffiles in directory - •Skips files already processed (
*-nobg.gif) - •Creates backups for all originals
- •Summary report at end
Example 3: Process with Custom Output Path
/remove-bg-gif input.gif output-transparent.gif
Result:
- •Processes
input.gif - •Saves result to
output-transparent.gif - •No automatic backup (manual path specified)
Example 4: Optimize After Processing
/remove-bg-gif --optimize public/mascot/main.gif
Result:
- •Removes background from
main.gif - •Automatically runs GIF optimization (lossy compression)
- •Target file size: <500KB
Skill Workflow
Phase 1: Preparation
- •Verify dependencies: Check Python packages installed
- •Analyze input: Read GIF metadata (frame count, duration, size)
- •Create backup: Save original as
{filename}.gif.backup - •Validate input: Ensure GIF is valid and processable
Phase 2: Frame-by-Frame Processing
- •Extract frames: Use
imageio.v3.imread()to get all frames - •Store metadata: Save duration, loop count, disposal method
- •Process each frame:
- •Convert frame to PNG bytes
- •Apply
rembg.remove()for background removal - •Convert back to PIL Image
- •Append to processed frames list
- •Progress reporting: Print
Frame X/Ystatus
Phase 3: GIF Reconstruction
- •Reassemble GIF: Use
imageio.v3.imwrite()with original timing - •Set loop count: Infinite loop (
loop=0) - •Preserve transparency: Ensure alpha channel maintained
- •Write output: Save to
{filename}-nobg.gif
Phase 4: Optimization (Optional)
- •Check file size: If >500KB, optimize
- •Apply compression: Use
gifsicle -O3 --lossy=80 - •Validate quality: Ensure animation still smooth
- •Report savings: Print file size before/after
Phase 5: Validation
- •Check output exists: Verify file created successfully
- •Validate GIF format: Use
filecommand - •Test animation: Open in browser, verify plays correctly
- •Check transparency: Verify background removed
- •Report results: Print summary (frame count, file size, duration)
Script Template
Location: scripts/remove_gif_background.py
Key Functions:
def extract_gif_frames(input_path: Path) -> tuple[list, dict]:
"""Extract all frames and metadata from GIF."""
frames = imageio.v3.imread(input_path, plugin='pillow')
metadata = imageio.v3.immeta(input_path)
return frames, metadata
def process_frame(frame: np.ndarray) -> Image:
"""Remove background from single frame."""
img = Image.fromarray(frame)
img_bytes = io.BytesIO()
img.save(img_bytes, format='PNG')
output_bytes = rembg.remove(img_bytes.getvalue())
return Image.open(io.BytesIO(output_bytes))
def reconstruct_gif(frames: list, metadata: dict, output_path: Path):
"""Reassemble frames into optimized GIF."""
imageio.v3.imwrite(
output_path,
frames,
duration=metadata.get('duration', 100),
loop=0,
optimize=True
)
def optimize_gif(input_path: Path, output_path: Path):
"""Optimize GIF file size using gifsicle."""
subprocess.run([
'gifsicle',
'-O3',
'--lossy=80',
str(input_path),
'-o', str(output_path)
])
Expected Results
Before Processing:
- •
main.gif- 1.6MB with colored background
After Processing:
- •
main-nobg.gif- 450KB with transparent background - •
main.gif.backup- Original preserved - •Console output:
code
Processing: main.gif Frame 1/120 Frame 2/120 ... Frame 120/120 ✓ Background removed ✓ Optimized: 1.6MB → 450KB ✓ Saved: main-nobg.gif
Troubleshooting
Issue: "ModuleNotFoundError: No module named 'rembg'"
Solution:
pip3 install rembg pillow imageio
Issue: GIF output is very large (>2MB)
Solution:
# Run optimization separately python3 scripts/optimize_processed_gifs.py public/mascot/*-nobg.gif
Issue: Animation timing is off after processing
Solution:
- •Check
metadata['duration']is preserved - •Some GIFs have variable frame durations (array, not single value)
- •Use
imageio.v3.imwrite()withdurationparameter as array
Issue: Transparency has white fringe/halo
Solution:
- •This is a limitation of
rembgmodel - •Manually touch up in image editor (GIMP, Photoshop)
- •Try different
rembgmodel:rembg.remove(input, model='u2net_human_seg')
Issue: Processing is very slow (>10 min per GIF)
Solution:
- •Normal for high frame count GIFs (100+ frames)
- •Process overnight or in background
- •Consider reducing frame count (keep every other frame) if acceptable
Integration with Mascot System
After processing GIFs, integrate into LexiClash:
- •
Update variant mapping in
components/ui/Mascot.tsx:typescriptconst GIF_VARIANTS = new Set(['happy', 'gaming', 'thinking', 'oops']);
- •
Add path helper:
typescriptexport function getMascotImagePath(variant: MascotVariant): string { if (GIF_VARIANTS.has(variant)) { const gifMap = { 'happy': '/mascot/main-nobg.gif', 'gaming': '/mascot/play-nobg.gif', 'thinking': '/mascot/study-nobg.gif', 'oops': '/mascot/oops-nobg.gif', }; return gifMap[variant]; } return MASCOT_IMAGES[variant]; } - •
Update components to use
getMascotImagePath()instead of directMASCOT_IMAGESlookup - •
Add
unoptimizedprop for Next.js Image components rendering GIFs
Performance Guidelines
Target Metrics:
- •File size: <500KB per GIF (after optimization)
- •Frame count: 30-120 frames ideal
- •Frame rate: 10-30 FPS
- •Processing time: 5-15 minutes per GIF acceptable
- •Animation smoothness: No visible stuttering
Optimization Tips:
- •Use lossy compression (80% quality is good balance)
- •Reduce color palette if possible (256 colors max)
- •Remove duplicate frames (if any exist)
- •Dithering can reduce file size for gradients
Quality Checklist
After processing, verify:
- • Background is fully transparent (no white/colored remnants)
- • Animation loops smoothly (no frame drops)
- • File size is acceptable (<500KB)
- • Transparency edges are clean (no halos/fringes)
- • Original animation timing preserved
- • GIF plays in all major browsers (Chrome, Firefox, Safari)
- • Works on dark backgrounds (LexiClash theme)
- • Backup of original exists (
.backupfile)
Skill Metadata
Version: 1.0.0 Author: LexiClash Development Team Created: 2026-01-19 Dependencies: Python 3.8+, rembg, pillow, imageio, gifsicle (optional) Estimated Time: 5-15 minutes per GIF (processing), 5 minutes (integration) Complexity: Medium (frame-by-frame processing requires care)
Related Skills
- •
/remove-bg- Static PNG background removal (simpler, faster) - •
/optimize-images- General image optimization - •
/generate-mascot- AI mascot image generation
Future Enhancements
Potential improvements:
- •Add WebP animated format support (better compression than GIF)
- •Parallel frame processing (multiprocessing for speed)
- •Smart frame reduction (remove duplicate frames automatically)
- •Quality analysis (detect halos/fringes automatically)
- •Batch processing with progress bar (tqdm library)
- •Integration with CI/CD (auto-process on commit)
Notes
Important caveats:
- •
rembgquality depends on model - human subjects work best - •Cartoon/illustrated mascots may have edge artifacts
- •Manual touch-up sometimes needed for perfect results
- •Processing is CPU-intensive (expect high CPU usage)
- •Memory usage scales with frame count (200MB+ typical)
Best practices:
- •Always create backups before processing
- •Process one GIF at a time initially (test quality)
- •Validate output before deleting original
- •Keep originals in version control or separate backup
- •Document any manual touch-ups for reproducibility