{"record":{"id":"5694f5b28d2b6304","repo":"huggingface/transformers","slug":"input-image-type-not-supported-type-image","errorCode":null,"errorMessage":"Input image type not supported: {type(image)}","messagePattern":"Input image type not supported: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/image_transforms.py","lineNumber":188,"sourceCode":"            and `False` otherwise.\n        image_mode (`str`, *optional*):\n            The mode to use for the PIL image. If unset, will use the default mode for the input image type.\n        input_data_format (`ChannelDimension`, *optional*):\n            The channel dimension format of the input image. If unset, will use the inferred format from the input.\n\n    Returns:\n        `PIL.Image.Image`: The converted image.\n    \"\"\"\n    requires_backends(to_pil_image, [\"vision\"])\n\n    if isinstance(image, PIL.Image.Image):\n        return image\n\n    # Convert all tensors to numpy arrays before converting to PIL image\n    if is_torch_tensor(image):\n        image = image.numpy()\n    elif not isinstance(image, np.ndarray):\n        raise ValueError(f\"Input image type not supported: {type(image)}\")\n\n    # If the channel has been moved to first dim, we put it back at the end.\n    image = to_channel_dimension_format(image, ChannelDimension.LAST, input_data_format)\n\n    # If there is a single channel, we squeeze it, as otherwise PIL can't handle it.\n    image = np.squeeze(image, axis=-1) if image.shape[-1] == 1 else image\n\n    # PIL.Image can only store uint8 values so we rescale the image to be between 0 and 255 if needed.\n    do_rescale = _rescale_for_pil_conversion(image) if do_rescale is None else do_rescale\n\n    if do_rescale:\n        image = rescale(image, 255)\n\n    image = image.astype(np.uint8)\n    return PIL.Image.fromarray(image, mode=image_mode)\n\n\ndef get_size_with_aspect_ratio(image_size, size, max_size=None) -> tuple[int, int]:","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/image_transforms.py#L170-L206","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\npil = to_pil_image('photo.png')  # raises ValueError\n\n# after\nfrom PIL import Image\npil = to_pil_image(Image.open('photo.png'))","handlingStrategy":"type-guard","validationCode":"import numpy as np\nfrom PIL import Image\nif isinstance(image, (str, bytes)):\n    image = Image.open(image)\nif isinstance(image, list):\n    image = image[0]\nif not isinstance(image, (np.ndarray, Image.Image)) and not is_torch_tensor(image):\n    raise TypeError(f\"unsupported image {type(image)}\")","typeGuard":"def is_supported_pil_input(x) -> bool:\n    import numpy as np\n    from PIL import Image\n    from transformers.utils import is_torch_tensor\n    return isinstance(x, (np.ndarray, Image.Image)) or is_torch_tensor(x)","tryCatchPattern":null,"preventionTips":["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."],"tags":["image-processing","pil","typeerror","input-validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}