huggingface/transformers · error · ValueError
Input image type not supported: {type(image)}
Error message
Input image type not supported: {type(image)} What it means
to_pil_image accepts PIL images, torch tensors, and numpy arrays only. After converting tensors to numpy, any other type (list, str, path, bytes) hits this ValueError. It is a boundary check before channel-format juggling and uint8 conversion.
Source
Thrown at src/transformers/image_transforms.py:188
and `False` otherwise.
image_mode (`str`, *optional*):
The mode to use for the PIL image. If unset, will use the default mode for the input image type.
input_data_format (`ChannelDimension`, *optional*):
The channel dimension format of the input image. If unset, will use the inferred format from the input.
Returns:
`PIL.Image.Image`: The converted image.
"""
requires_backends(to_pil_image, ["vision"])
if isinstance(image, PIL.Image.Image):
return image
# Convert all tensors to numpy arrays before converting to PIL image
if is_torch_tensor(image):
image = image.numpy()
elif not isinstance(image, np.ndarray):
raise ValueError(f"Input image type not supported: {type(image)}")
# If the channel has been moved to first dim, we put it back at the end.
image = to_channel_dimension_format(image, ChannelDimension.LAST, input_data_format)
# If there is a single channel, we squeeze it, as otherwise PIL can't handle it.
image = np.squeeze(image, axis=-1) if image.shape[-1] == 1 else image
# PIL.Image can only store uint8 values so we rescale the image to be between 0 and 255 if needed.
do_rescale = _rescale_for_pil_conversion(image) if do_rescale is None else do_rescale
if do_rescale:
image = rescale(image, 255)
image = image.astype(np.uint8)
return PIL.Image.fromarray(image, mode=image_mode)
def get_size_with_aspect_ratio(image_size, size, max_size=None) -> tuple[int, int]:View on GitHub (pinned to a597f97485)
Solutions
- Load files with PIL first: to_pil_image(PIL.Image.open(path)).
- Unwrap lists: to_pil_image(images[0]).
- Convert other array types to numpy before calling.
Example fix
# before
pil = to_pil_image('photo.png') # raises ValueError
# after
from PIL import Image
pil = to_pil_image(Image.open('photo.png')) Defensive patterns
Strategy: type-guard
Validate before calling
import numpy as np
from PIL import Image
if isinstance(image, (str, bytes)):
image = Image.open(image)
if isinstance(image, list):
image = image[0]
if not isinstance(image, (np.ndarray, Image.Image)) and not is_torch_tensor(image):
raise TypeError(f"unsupported image {type(image)}") Type guard
def is_supported_pil_input(x) -> bool:
import numpy as np
from PIL import Image
from transformers.utils import is_torch_tensor
return isinstance(x, (np.ndarray, Image.Image)) or is_torch_tensor(x) Prevention
- Load files with PIL.Image.open at the data layer, never pass paths to transform functions.
- Index single images out of batched lists before conversion.
When it happens
Trigger: to_pil_image([np_image]) (image wrapped in a list), to_pil_image('image.png'), to_pil_image(raw_bytes), or passing a tf.Tensor in an environment where it is not a torch tensor.
Common situations: Batch loops that accidentally forward a whole list of images, file-path confusion (thinking the function loads files), or non-torch tensor types leaking in.
Related errors
- Input image must be of type np.ndarray, got {type(image)}
- The image to be converted to a PIL image contains values out
- The image to be converted to a PIL image contains values out
- image must be a numpy array
- Invalid input type. Must be a single audio or a list of audi
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/5694f5b28d2b6304.
Report an issue: GitHub.