# Image

## utils/image

Helper module for image processing.

These functions and classes are only used internally, meaning an end-user shouldn’t need to access anything here.

* [utils/image](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image)
  * [.RawImage](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage)
    * [`new RawImage(data, width, height, channels)`](https://huggingface.co/docs/transformers.js/api/utils/image#new_module_utils/image.RawImage_new)
    * *instance*
      * [`._CONTENT_TYPE_MAP`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+_CONTENT_TYPE_MAP)
      * [`.grayscale()`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+grayscale) ⇒ `RawImage`
      * [`.rgb()`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+rgb) ⇒ `RawImage`
      * [`.rgba()`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+rgba) ⇒ `RawImage`
      * [`.resize(width, height, options)`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+resize) ⇒ `Promise.<RawImage>`
      * [`.clone()`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+clone) ⇒ `RawImage`
      * [`.convert(numChannels)`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+convert) ⇒ `RawImage`
      * [`.save(path)`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+save)
    * *static*
      * [`.read(input)`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage.read) ⇒ `*`
      * [`.fromURL(url)`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage.fromURL) ⇒ `Promise.<RawImage>`
      * [`.fromBlob(blob)`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage.fromBlob) ⇒ `Promise.<RawImage>`

***

### utils/image.RawImage

**Kind**: static class of [`utils/image`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image)

* [.RawImage](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage)
  * [`new RawImage(data, width, height, channels)`](https://huggingface.co/docs/transformers.js/api/utils/image#new_module_utils/image.RawImage_new)
  * *instance*
    * [`._CONTENT_TYPE_MAP`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+_CONTENT_TYPE_MAP)
    * [`.grayscale()`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+grayscale) ⇒ `RawImage`
    * [`.rgb()`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+rgb) ⇒ `RawImage`
    * [`.rgba()`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+rgba) ⇒ `RawImage`
    * [`.resize(width, height, options)`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+resize) ⇒ `Promise.<RawImage>`
    * [`.clone()`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+clone) ⇒ `RawImage`
    * [`.convert(numChannels)`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+convert) ⇒ `RawImage`
    * [`.save(path)`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage+save)
  * *static*
    * [`.read(input)`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage.read) ⇒ `*`
    * [`.fromURL(url)`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage.fromURL) ⇒ `Promise.<RawImage>`
    * [`.fromBlob(blob)`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage.fromBlob) ⇒ `Promise.<RawImage>`

***

#### new RawImage(data, width, height, channels)

Create a new `RawImage` object.

| Param    | Type                     | Description              |
| -------- | ------------------------ | ------------------------ |
| data     | `Uint8ClampedArray`      | The pixel data.          |
| width    | `number`                 | The width of the image.  |
| height   | `number`                 | The height of the image. |
| channels | `1` \| `2` \| `3` \| `4` | The number of channels.  |

***

#### rawImage.\_CONTENT\_TYPE\_MAP

Mapping from file extensions to MIME types.

**Kind**: instance property of [`RawImage`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage)

***

#### rawImage.grayscale() ⇒ \<code> RawImage \</code>

Convert the image to grayscale format.

**Kind**: instance method of [`RawImage`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage)\
**Returns**: `RawImage` - `this` to support chaining.

***

#### rawImage.rgb() ⇒ \<code> RawImage \</code>

Convert the image to RGB format.

**Kind**: instance method of [`RawImage`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage)\
**Returns**: `RawImage` - `this` to support chaining.

***

#### rawImage.rgba() ⇒ \<code> RawImage \</code>

Convert the image to RGBA format.

**Kind**: instance method of [`RawImage`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage)\
**Returns**: `RawImage` - `this` to support chaining.

***

#### rawImage.resize(width, height, options) ⇒ \<code> Promise. < RawImage > \</code>

Resize the image to the given dimensions. This method uses the canvas API to perform the resizing.

**Kind**: instance method of [`RawImage`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage)\
**Returns**: `Promise.<RawImage>` - `this` to support chaining.

| Param               | Type                                               | Description                      |
| ------------------- | -------------------------------------------------- | -------------------------------- |
| width               | `number`                                           | The width of the new image.      |
| height              | `number`                                           | The height of the new image.     |
| options             | `Object`                                           | Additional options for resizing. |
| \[options.resample] | `0` \| `1` \| `2` \| `3` \| `4` \| `5` \| `string` | The resampling method to use.    |

***

#### rawImage.clone() ⇒ \<code> RawImage \</code>

Clone the image

**Kind**: instance method of [`RawImage`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage)\
**Returns**: `RawImage` - The cloned image

***

#### rawImage.convert(numChannels) ⇒ \<code> RawImage \</code>

Helper method for converting image to have a certain number of channels

**Kind**: instance method of [`RawImage`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage)\
**Returns**: `RawImage` - `this` to support chaining.

| Param       | Type     | Description                                 |
| ----------- | -------- | ------------------------------------------- |
| numChannels | `number` | The number of channels. Must be 1, 3, or 4. |

***

#### rawImage.save(path)

Save the image to the given path.

**Kind**: instance method of [`RawImage`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage)

| Param | Type     | Description                    |
| ----- | -------- | ------------------------------ |
| path  | `string` | The path to save the image to. |

***

#### RawImage.read(input) ⇒ \<code> \* \</code>

Helper method for reading an image from a variety of input types.

**Kind**: static method of [`RawImage`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage)\
**Returns**: `*` - The image object.

**Example:** Read image from a URL.

Copied

```
let image = await RawImage.read('https://boincai.com/datasets/Xenova/transformers.js-docs/resolve/main/football-match.jpg');
// RawImage 
```

| Param | Type                            |
| ----- | ------------------------------- |
| input | `RawImage` \| `string` \| `URL` |

***

#### RawImage.fromURL(url) ⇒ \<code> Promise. < RawImage > \</code>

Read an image from a URL or file path.

**Kind**: static method of [`RawImage`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage)\
**Returns**: `Promise.<RawImage>` - The image object.

| Param | Type              | Description                                  |
| ----- | ----------------- | -------------------------------------------- |
| url   | `string` \| `URL` | The URL or file path to read the image from. |

***

#### RawImage.fromBlob(blob) ⇒ \<code> Promise. < RawImage > \</code>

Helper method to create a new Image from a blob.

**Kind**: static method of [`RawImage`](https://huggingface.co/docs/transformers.js/api/utils/image#module_utils/image.RawImage)\
**Returns**: `Promise.<RawImage>` - The image object.

| Param | Type   | Description                      |
| ----- | ------ | -------------------------------- |
| blob  | `Blob` | The blob to read the image from. |
