Utilities for Image Processors

This page lists all the utility functions used by the image processors, mainly the functional transformations used to process the images.

Most of those are only useful if you are studying the code of the image processors in the library.

## [](#transformers.image_transforms.center_crop)Image Transformations

#### transformers.image\_transforms.center\_crop

[](#transformers.image_transforms.center_crop)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_transforms.py#L390)

( image: ndarraysize: typing.Tuple\[int, int\]data\_format: typing.Union\[transformers.image\_utils.ChannelDimension, str, NoneType\] = Nonereturn\_numpy: typing.Optional\[bool\] = None ) → `np.ndarray`

Crops the `image` to the specified `size` using a center crop. Note that if the image is too small to be cropped to the size given, it will be padded (so the returned result will always be of size `size`).

#### transformers.image\_transforms.center\_to\_corners\_format

[](#transformers.image_transforms.center_to_corners_format)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_transforms.py#L518)

( bboxes\_center: TensorType )

Converts bounding boxes from center format to corners format.

center format: contains the coordinate for the center of the box and its width, height dimensions (center\_x, center\_y, width, height) corners format: contains the coodinates for the top-left and bottom-right corners of the box (top\_left\_x, top\_left\_y, bottom\_right\_x, bottom\_right\_y)

#### transformers.image\_transforms.corners\_to\_center\_format

[](#transformers.image_transforms.corners_to_center_format)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_transforms.py#L578)

( bboxes\_corners: TensorType )

Converts bounding boxes from corners format to center format.

corners format: contains the coodinates for the top-left and bottom-right corners of the box (top\_left\_x, top\_left\_y, bottom\_right\_x, bottom\_right\_y) center format: contains the coordinate for the center of the box and its the width, height dimensions (center\_x, center\_y, width, height)

#### transformers.image\_transforms.id\_to\_rgb

[](#transformers.image_transforms.id_to_rgb)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_transforms.py#L612)

( id\_map )

Converts unique ID to RGB color.

#### transformers.image\_transforms.normalize

[](#transformers.image_transforms.normalize)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_transforms.py#L327)

( image: ndarraymean: typing.Union\[float, typing.Iterable\[float\]\]std: typing.Union\[float, typing.Iterable\[float\]\]data\_format: typing.Optional\[transformers.image\_utils.ChannelDimension\] = None )

Parameters

-   [](#transformers.image_transforms.normalize.image)**image** (`np.ndarray`) — The image to normalize.
-   [](#transformers.image_transforms.normalize.mean)**mean** (`float` or `Iterable[float]`) — The mean to use for normalization.
-   [](#transformers.image_transforms.normalize.std)**std** (`float` or `Iterable[float]`) — The standard deviation to use for normalization.
-   [](#transformers.image_transforms.normalize.data_format)**data\_format** (`ChannelDimension`, _optional_) — The channel dimension format of the output image. If unset, will use the inferred format from the input.

Normalizes `image` using the mean and standard deviation specified by `mean` and `std`.

image = (image - mean) / std

#### transformers.image\_transforms.pad

[](#transformers.image_transforms.pad)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_transforms.py#L642)

( image: ndarraypadding: typing.Union\[int, typing.Tuple\[int, int\], typing.Iterable\[typing.Tuple\[int, int\]\]\]mode: PaddingMode = <PaddingMode.CONSTANT: 'constant'>constant\_values: typing.Union\[float, typing.Iterable\[float\]\] = 0.0data\_format: typing.Union\[transformers.image\_utils.ChannelDimension, str, NoneType\] = Noneinput\_data\_format: typing.Union\[transformers.image\_utils.ChannelDimension, str, NoneType\] = None ) → `np.ndarray`

Pads the `image` with the specified (height, width) `padding` and `mode`.

#### transformers.image\_transforms.rgb\_to\_id

[](#transformers.image_transforms.rgb_to_id)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_transforms.py#L601)

( color )

Converts RGB color to unique ID.

#### transformers.image\_transforms.rescale

[](#transformers.image_transforms.rescale)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_transforms.py#L91)

( image: ndarrayscale: floatdata\_format: typing.Optional\[transformers.image\_utils.ChannelDimension\] = Nonedtype = <class 'numpy.float32'> ) → `np.ndarray`

Parameters

-   [](#transformers.image_transforms.rescale.image)**image** (`np.ndarray`) — The image to rescale.
-   [](#transformers.image_transforms.rescale.scale)**scale** (`float`) — The scale to use for rescaling the image.
-   [](#transformers.image_transforms.rescale.data_format)**data\_format** (`ChannelDimension`, _optional_) — The channel dimension format of the image. If not provided, it will be the same as the input image.
-   [](#transformers.image_transforms.rescale.dtype)**dtype** (`np.dtype`, _optional_, defaults to `np.float32`) — The dtype of the output image. Defaults to `np.float32`. Used for backwards compatibility with feature extractors.

The rescaled image.

Rescales `image` by `scale`.

#### transformers.image\_transforms.resize

[](#transformers.image_transforms.resize)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_transforms.py#L261)

( imagesize: typing.Tuple\[int, int\]resample: PILImageResampling = Nonereducing\_gap: typing.Optional\[int\] = Nonedata\_format: typing.Optional\[transformers.image\_utils.ChannelDimension\] = Nonereturn\_numpy: bool = True ) → `np.ndarray`

Resizes `image` to `(height, width)` specified by `size` using the PIL library.

#### transformers.image\_transforms.to\_pil\_image

[](#transformers.image_transforms.to_pil_image)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_transforms.py#L148)

( image: typing.Union\[numpy.ndarray, ForwardRef('PIL.Image.Image'), ForwardRef('torch.Tensor'), ForwardRef('tf.Tensor'), ForwardRef('jnp.ndarray')\]do\_rescale: typing.Optional\[bool\] = None ) → `PIL.Image.Image`

Parameters

-   [](#transformers.image_transforms.to_pil_image.image)**image** (`PIL.Image.Image` or `numpy.ndarray` or `torch.Tensor` or `tf.Tensor`) — The image to convert to the `PIL.Image` format.
-   [](#transformers.image_transforms.to_pil_image.do_rescale)**do\_rescale** (`bool`, _optional_) — Whether or not to apply the scaling factor (to make pixel values integers between 0 and 255). Will default to `True` if the image type is a floating type and casting to `int` would result in a loss of precision, and `False` otherwise.

The converted image.

Converts `image` to a PIL Image. Optionally rescales it and puts the channel dimension back as the last axis if needed.

## [](#transformers.ImageProcessingMixin)ImageProcessingMixin

### class transformers.ImageProcessingMixin

[](#transformers.ImageProcessingMixin)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L59)

( \*\*kwargs )

This is an image processor mixin used to provide saving/loading functionality for sequential and image feature extractors.

#### from\_dict

[](#transformers.ImageProcessingMixin.from_dict)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L320)

( image\_processor\_dict: typing.Dict\[str, typing.Any\]\*\*kwargs ) → [ImageProcessingMixin](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin)

Parameters

-   [](#transformers.ImageProcessingMixin.from_dict.image_processor_dict)**image\_processor\_dict** (`Dict[str, Any]`) — Dictionary that will be used to instantiate the image processor object. Such a dictionary can be retrieved from a pretrained checkpoint by leveraging the [to\_dict()](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin.to_dict) method.
-   [](#transformers.ImageProcessingMixin.from_dict.kwargs)**kwargs** (`Dict[str, Any]`) — Additional parameters from which to initialize the image processor object.

The image processor object instantiated from those parameters.

Instantiates a type of [ImageProcessingMixin](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin) from a Python dictionary of parameters.

#### from\_json\_file

[](#transformers.ImageProcessingMixin.from_json_file)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L377)

( json\_file: typing.Union\[str, os.PathLike\] ) → A image processor of type [ImageProcessingMixin](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin)

Parameters

-   [](#transformers.ImageProcessingMixin.from_json_file.json_file)**json\_file** (`str` or `os.PathLike`) — Path to the JSON file containing the parameters.

Returns

A image processor of type [ImageProcessingMixin](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin)

The image\_processor object instantiated from that JSON file.

Instantiates a image processor of type [ImageProcessingMixin](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin) from the path to a JSON file of parameters.

#### from\_pretrained

[](#transformers.ImageProcessingMixin.from_pretrained)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L83)

( pretrained\_model\_name\_or\_path: typing.Union\[str, os.PathLike\]\*\*kwargs )

Instantiate a type of [ImageProcessingMixin](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin) from an image processor.

Examples:

```

image_processor = CLIPImageProcessor.from_pretrained(
    "openai/clip-vit-base-patch32"
)  
image_processor = CLIPImageProcessor.from_pretrained(
    "./test/saved_model/"
)  
image_processor = CLIPImageProcessor.from_pretrained("./test/saved_model/preprocessor_config.json")
image_processor = CLIPImageProcessor.from_pretrained(
    "openai/clip-vit-base-patch32", do_normalize=False, foo=False
)
assert image_processor.do_normalize is False
image_processor, unused_kwargs = CLIPImageProcessor.from_pretrained(
    "openai/clip-vit-base-patch32", do_normalize=False, foo=False, return_unused_kwargs=True
)
assert image_processor.do_normalize is False
assert unused_kwargs == {"foo": False}```

#### get\_image\_processor\_dict

[](#transformers.ImageProcessingMixin.get_image_processor_dict)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L217)

( pretrained\_model\_name\_or\_path: typing.Union\[str, os.PathLike\]\*\*kwargs ) → `Tuple[Dict, Dict]`

Parameters

-   [](#transformers.ImageProcessingMixin.get_image_processor_dict.pretrained_model_name_or_path)**pretrained\_model\_name\_or\_path** (`str` or `os.PathLike`) — The identifier of the pre-trained checkpoint from which we want the dictionary of parameters.
-   [](#transformers.ImageProcessingMixin.get_image_processor_dict.subfolder)**subfolder** (`str`, _optional_, defaults to `""`) — In case the relevant files are located inside a subfolder of the model repo on huggingface.co, you can specify the folder name here.

Returns

`Tuple[Dict, Dict]`

The dictionary(ies) that will be used to instantiate the image processor object.

From a `pretrained_model_name_or_path`, resolve to a dictionary of parameters, to be used for instantiating a image processor of type `~image_processor_utils.ImageProcessingMixin` using `from_dict`.

#### push\_to\_hub

[](#transformers.ImageProcessingMixin.push_to_hub)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/utils/hub.py#L734)

( repo\_id: struse\_temp\_dir: typing.Optional\[bool\] = Nonecommit\_message: typing.Optional\[str\] = Noneprivate: typing.Optional\[bool\] = Noneuse\_auth\_token: typing.Union\[bool, str, NoneType\] = Nonemax\_shard\_size: typing.Union\[int, str, NoneType\] = '10GB'create\_pr: bool = Falsesafe\_serialization: bool = False\*\*deprecated\_kwargs )

Upload the image processor file to the 🤗 Model Hub while synchronizing a local clone of the repo in `repo_path_or_name`.

Examples:

```
from transformers import AutoImageProcessor

image processor = AutoImageProcessor.from_pretrained("bert-base-cased")


image processor.push_to_hub("my-finetuned-bert")


image processor.push_to_hub("huggingface/my-finetuned-bert")```

#### register\_for\_auto\_class

[](#transformers.ImageProcessingMixin.register_for_auto_class)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L431)

( auto\_class = 'AutoImageProcessor' )

Parameters

-   [](#transformers.ImageProcessingMixin.register_for_auto_class.auto_class)**auto\_class** (`str` or `type`, _optional_, defaults to `"AutoImageProcessor "`) — The auto class to register this new image processor with.

Register this class with a given auto class. This should only be used for custom image processors as the ones in the library are already mapped with `AutoImageProcessor` .

This API is experimental and may have some slight breaking changes in the next releases.

#### save\_pretrained

[](#transformers.ImageProcessingMixin.save_pretrained)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L169)

( save\_directory: typing.Union\[str, os.PathLike\]push\_to\_hub: bool = False\*\*kwargs )

Parameters

-   [](#transformers.ImageProcessingMixin.save_pretrained.save_directory)**save\_directory** (`str` or `os.PathLike`) — Directory where the image processor JSON file will be saved (will be created if it does not exist).
-   [](#transformers.ImageProcessingMixin.save_pretrained.push_to_hub)**push\_to\_hub** (`bool`, _optional_, defaults to `False`) — Whether or not to push your model to the Hugging Face model hub after saving it. You can specify the repository you want to push to with `repo_id` (will default to the name of `save_directory` in your namespace). kwargs — Additional key word arguments passed along to the [push\_to\_hub()](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin.push_to_hub) method.

Save an image processor object to the directory `save_directory`, so that it can be re-loaded using the [from\_pretrained()](/docs/transformers/v4.30.0/en/internal/image_processing_utils#transformers.ImageProcessingMixin.from_pretrained) class method.

#### to\_dict

[](#transformers.ImageProcessingMixin.to_dict)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L365)

( ) → `Dict[str, Any]`

Dictionary of all the attributes that make up this image processor instance.

Serializes this instance to a Python dictionary.

#### to\_json\_file

[](#transformers.ImageProcessingMixin.to_json_file)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L417)

( json\_file\_path: typing.Union\[str, os.PathLike\] )

Parameters

-   [](#transformers.ImageProcessingMixin.to_json_file.json_file_path)**json\_file\_path** (`str` or `os.PathLike`) — Path to the JSON file in which this image\_processor instance’s parameters will be saved.

Save this instance to a JSON file.

#### to\_json\_string

[](#transformers.ImageProcessingMixin.to_json_string)[< source \>](https://github.com/huggingface/transformers/blob/v4.30.0/src/transformers/image_processing_utils.py#L396)

( ) → `str`

String containing all the attributes that make up this feature\_extractor instance in JSON format.

Serializes this instance to a JSON string.