picture-converter
v1.0.1
Published
Convert PNG, JPG, and GIF images to modern WebP and AVIF formats using sharp.
Maintainers
Readme
picture-converter
Convert old image formats (.png, .jpg, .gif) to modern, efficient formats (.webp, .avif) automatically.
Built with sharp — fast, reliable, and production-ready.
🚀 Features
- 🔍 Recursively scans folders for images
- 🧠 Skips already converted files (unless forced)
- ⚙️ Configurable encoding options for WebP and AVIF
- 🪶 Handles GIF → WebP animation conversion
📦 Installation
npm install picture-converter🧑💻 Usage
Import and run
import convertImages from 'picture-converter'
await convertImages('./images')Conversions:
.pngand.jpg→.webpand.avif.gif→.webp- Existing
.webpand.aviffiles are skipped unless you setforceRewritetotrue.
With Options
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 |
Example Folder Structure
Before:
images/
├── photo.jpg
├── logo.png
└── funny.gifAfter:
images/
├── photo.jpg
├── photo.webp
├── photo.avif
├── logo.png
├── logo.webp
├── logo.avif
├── funny.gif
└── funny.webp🧠 How It Works
- 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.
⚙️ API Reference
convertImages(
inputDir: string,
conversionOptions?: {
animated?: boolean
webp?: sharp.WebpOptions
avif?: sharp.AvifOptions
},
forceRewrite?: boolean
): Promise<{converted: number, skipped: number, failed: number}>Contributing
Contributions, issues, and feature requests are welcome!
Open an issue or submit a
pull request.
