> 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.

# Image

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

| Language | Package                | Module                 |
| -------- | ---------------------- | ---------------------- |
| Node.js  | `@swc/anyformat-image` | `@swc/anyformat-image` |
| Python   | `anyformat-image`      | `anyformat_image`      |

## Supported formats

| Identifier | MIME type                  | Format                                                    |
| ---------- | -------------------------- | --------------------------------------------------------- |
| `png`      | `image/png`                | Portable Network Graphics                                 |
| `jpeg`     | `image/jpeg`               | JPEG (JFIF or Exif)                                       |
| `gif`      | `image/gif`                | Graphics Interchange Format                               |
| `webp`     | `image/webp`               | WebP                                                      |
| `avif`     | `image/avif`               | AV1 Image File Format                                     |
| `heic`     | `image/heic`               | High Efficiency Image File Format with HEVC coding        |
| `jxl`      | `image/jxl`                | JPEG XL, as a bare codestream or in an ISO BMFF container |
| `tiff`     | `image/tiff`               | Tagged Image File Format, including BigTIFF               |
| `bmp`      | `image/bmp`                | Windows bitmap                                            |
| `ico`      | `image/vnd.microsoft.icon` | Windows icon                                              |
| `qoi`      | `image/qoi`                | Quite 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

```ts
function detect(data: Uint8Array): DetectedFormat | null
```

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

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

### Python

```python
def detect(data: bytes) -> DetectedFormat | None
```

```python
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.js    | Python      | Type     | Description                                                         |
| ---------- | ----------- | -------- | ------------------------------------------------------------------- |
| `format`   | `format`    | `string` | The stable lowercase identifier of the format, for example `"png"`. |
| `mimeType` | `mime_type` | `string` | The 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

```ts
function supportedFormats(): string[]
```

```js
import { supportedFormats } from '@swc/anyformat-image'

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

### Python

```python
def supported_formats() -> list[str]
```

```python
import anyformat_image

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