For AI agents: the complete documentation index is available at https://anyformat.swc.rs/llms.txt, the full documentation bundle is available at https://anyformat.swc.rs/llms-full.txt, and this page is available as Markdown at https://anyformat.swc.rs/guide/getting-started.md.

Getting started

Requirements

  • Node.js 18 or later, for the Node.js packages.
  • Python 3.10 or later, for the Python packages.

Install a package

Install the package of each format family that you use. The examples on this page use the image family.

Node.js
Python
npm
yarn
pnpm
bun
deno
npm add @swc/anyformat-image

Detect the format of a file

Give the bytes of a file to detect. The function needs only the first bytes of the file. 64 bytes are enough for most image files.

Node.js
Python
import { readFile } from 'node:fs/promises'
import { detect } from '@swc/anyformat-image'

detect(await readFile('photo.avif'))
// => { format: 'avif', mimeType: 'image/avif' }

When the format is not recognized, detect returns null in Node.js and None in Python.

List the supported formats

supportedFormats returns the identifier of each format that detect can return.

Node.js
Python
import { supportedFormats } from '@swc/anyformat-image'

supportedFormats()
// => ['png', 'jpeg', 'gif', 'webp', 'avif', 'heic', 'jxl', 'tiff', 'bmp', 'ico', 'qoi']

Compare two images

compareImages decodes two PNG files, compares them with the algorithm of pixelmatch, and encodes the diff image.

Node.js
Python
import { readFile, writeFile } from 'node:fs/promises'
import { compareImages } from '@swc/anyformat-image'

const result = await compareImages(
  await readFile('expected.png'),
  await readFile('actual.png'),
  { threshold: 0.1, diff: { format: 'png' } },
)
if (result.diffPixels > 0) await writeFile('diff.png', result.diffEncoded)

Next steps