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/api/image.md.

Image

The image family identifies image formats from the leading bytes of a file.

LanguagePackageModule
Node.js@swc/anyformat-image@swc/anyformat-image
Pythonanyformat-imageanyformat_image

Supported formats

IdentifierMIME typeFormat
pngimage/pngPortable Network Graphics
jpegimage/jpegJPEG (JFIF or Exif)
gifimage/gifGraphics Interchange Format
webpimage/webpWebP
avifimage/avifAV1 Image File Format
heicimage/heicHigh Efficiency Image File Format with HEVC coding
jxlimage/jxlJPEG XL, as a bare codestream or in an ISO BMFF container
tiffimage/tiffTagged Image File Format, including BigTIFF
bmpimage/bmpWindows bitmap
icoimage/vnd.microsoft.iconWindows icon
qoiimage/qoiQuite OK Image Format

detect

Identifies the image format of data from its leading bytes. Returns null in Node.js and None in Python when the format is not recognized.

The first 64 bytes of a file are enough for every image format.

Node.js

function detect(data: Uint8Array): DetectedFormat | null
import { readFile } from 'node:fs/promises'
import { detect } from '@swc/anyformat-image'

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

Python

def detect(data: bytes) -> DetectedFormat | None
from pathlib import Path

import anyformat_image

anyformat_image.detect(Path("photo.avif").read_bytes())
# => DetectedFormat(format='avif', mime_type='image/avif')

DetectedFormat

A format identified by detect.

Node.jsPythonTypeDescription
formatformatstringThe stable lowercase identifier of the format, for example "png".
mimeTypemime_typestringThe most common MIME type of the format.

In Python, DetectedFormat is a final class with read-only properties, and the type of each property is str.

supportedFormats

Returns the identifier of each format that detect can return.

Node.js

function supportedFormats(): string[]
import { supportedFormats } from '@swc/anyformat-image'

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

Python

def supported_formats() -> list[str]
import anyformat_image

anyformat_image.supported_formats()
# => ['png', 'jpeg', 'gif', 'webp', 'avif', 'heic', 'jxl', 'tiff', 'bmp', 'ico', 'qoi']