AI Operations
Node Browser AIAI-powered transforms. No API key required — all models run locally via ONNX.
AI models are lazy-loaded on first use. Expect a short delay on the first call as the model downloads and initialises. Subsequent calls are fast.
removeBackground()
.removeBackground(): Pipeline.removeBackground(): PipelineRemove the image background using a U2-Net ONNX model. Outputs a PNG with transparent background (alpha channel).
const png = await img('portrait.jpg')
.removeBackground()
.toBuffer()
// In browser
const dataUrl = await img(file)
.removeBackground()
.toDataURL()const png = await img('portrait.jpg')
.removeBackground()
.toBuffer()
// In browser
const dataUrl = await img(file)
.removeBackground()
.toDataURL()The output format is always PNG when background removal is used, regardless of the downstream format operation. Chain a format conversion if you need a specific output:
await img('photo.jpg')
.removeBackground()
.resize(800)
.png()
.toBuffer()await img('photo.jpg')
.removeBackground()
.resize(800)
.png()
.toBuffer()smartCrop()
.smartCrop(subject?: 'face' | 'object'): Pipeline.smartCrop(subject?: 'face' | 'object'): PipelineContent-aware cropping using COCO-SSD object detection. Identifies the primary subject and crops to keep it centred.
| Prop | Type | Default | Description |
|---|---|---|---|
| subject | 'face' | 'object' | 'object' | Hint for detection model. 'face' prioritises face regions. |
// Crop to 400x400 keeping the main subject
await img('portrait.jpg')
.smartCrop('face')
.resize(400, 400, { fit: 'cover' })
.webp()
.toBuffer()// Crop to 400x400 keeping the main subject
await img('portrait.jpg')
.smartCrop('face')
.resize(400, 400, { fit: 'cover' })
.webp()
.toBuffer()Chain smartCrop() before resize(). The crop happens first, then the resize scales the result to your target dimensions.
upscale()
.upscale(factor?: 2 | 4): Pipeline.upscale(factor?: 2 | 4): PipelineAI upscaling using ESRGAN. Increases resolution while recovering detail that conventional bicubic interpolation loses.
| Prop | Type | Default | Description |
|---|---|---|---|
| factor | 2 | 4 | 2 | Upscale multiplier. 2x doubles dimensions, 4x quadruples. |
// 2x upscale (default)
await img('low-res.jpg').upscale().toBuffer()
// 4x upscale
await img('thumbnail.jpg').upscale(4).toBuffer()// 2x upscale (default)
await img('low-res.jpg').upscale().toBuffer()
// 4x upscale
await img('thumbnail.jpg').upscale(4).toBuffer()4x upscaling is memory-intensive. For images above 1MP, prefer 2x and run twice if needed.
Combining AI ops
// Remove background, upscale, export
await img('product.jpg')
.removeBackground()
.upscale(2)
.png()
.toBuffer()// Remove background, upscale, export
await img('product.jpg')
.removeBackground()
.upscale(2)
.png()
.toBuffer()