Table of Contents
Shortcuts

# Source code for torchvision.transforms.v2.functional._type_conversion

    from typing import Union

    import numpy as np
    import PIL.Image
    import torch
    from torchvision import tv_tensors
    from torchvision.transforms import functional as _F

    @torch.jit.unused
    def to_image(inpt: Union[torch.Tensor, PIL.Image.Image, np.ndarray]) -> tv_tensors.Image:
        """[BETA] See :class:`~torchvision.transforms.v2.ToImage` for details."""
        if isinstance(inpt, np.ndarray):
            output = torch.from_numpy(inpt).permute((2, 0, 1)).contiguous()
        elif isinstance(inpt, PIL.Image.Image):
            output = pil_to_tensor(inpt)
        elif isinstance(inpt, torch.Tensor):
            output = inpt
        else:
            raise TypeError(f"Input can either be a numpy array or a PIL image, but got {type(inpt)} instead.")
        return tv_tensors.Image(output)

    to_pil_image = _F.to_pil_image
    pil_to_tensor = _F.pil_to_tensor

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.