Convert old image formats (.png, .jpg, .gif) to modern, efficient formats (.webp, .avif) automatically.
Built with sharp — fast, reliable, and production-ready.
- 🔍 Recursively scans folders for images
- 🧠 Skips already converted files (unless forced)
- ⚙️ Configurable encoding options for WebP and AVIF
- 🪶 Handles GIF → WebP animation conversion
npm install picture-converterimport convertImages from 'picture-converter'
await convertImages('./images')Conversions:
.pngand.jpg→.webpand.avif.gif→.webp- Existing
.webpand.aviffiles are skipped unless you setforceRewritetotrue.
await convertImages('./assets', {
animated: true,
webp: {quality: 85},
avif: {quality: 50, effort: 6},
}, true)Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
inputDir |
string |
required | Directory containing images |
conversionOptions |
{animated?: boolean, webp?: WebpOptions; avif?: AvifOptions} |
{} |
Passed directly to sharp |
forceRewrite |
boolean |
false |
Whether to overwrite existing .webp / .avif |
Before:
images/
├── photo.jpg
├── logo.png
└── funny.gif
After:
images/
├── photo.jpg
├── photo.webp
├── photo.avif
├── logo.png
├── logo.webp
├── logo.avif
├── funny.gif
└── funny.webp
- Scans directories recursively.
- Detects base formats (
.jpg,.png,.gif). - Checks whether
.webp/.avifversions already exist. - Converts missing ones using
sharp. - Skips or overwrites depending on
forceRewrite.
convertImages(
inputDir: string,
conversionOptions?: {
animated?: boolean
webp?: sharp.WebpOptions
avif?: sharp.AvifOptions
},
forceRewrite?: boolean
): Promise<{converted: number, skipped: number, failed: number}>Contributions, issues, and feature requests are welcome!
Open an issue or submit a
pull request.