{"record":{"id":"d920f64e7bf1ce3b","repo":"huggingface/transformers","slug":"incorrect-format-used-for-image-should-be-an-url","errorCode":null,"errorMessage":"Incorrect format used for image. Should be an url linking to an image, a base64 string, a local path, or a PIL image.","messagePattern":"Incorrect format used for image\\. Should be an url linking to an image, a base64 string, a local path, or a PIL image\\.","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/transformers/image_utils.py","lineNumber":507,"sourceCode":"            # We need to actually check for a real protocol, otherwise it's impossible to use a local file\n            # like http_huggingface_co.png\n            image = PIL.Image.open(BytesIO(httpx.get(image, timeout=timeout, follow_redirects=True).content))\n        elif os.path.isfile(image):\n            image = PIL.Image.open(image)\n        else:\n            if image.startswith(\"data:image/\"):\n                image = image.split(\",\")[1]\n\n            # Try to load as base64\n            try:\n                b64 = base64.decodebytes(image.encode())\n                image = PIL.Image.open(BytesIO(b64))\n            except Exception as e:\n                raise ValueError(\n                    f\"Incorrect image source. Must be a valid URL starting with `http://` or `https://`, a valid path to an image file, or a base64 encoded string. Got {image}. Failed with {e}\"\n                )\n    elif not isinstance(image, PIL.Image.Image):\n        raise TypeError(\n            \"Incorrect format used for image. Should be an url linking to an image, a base64 string, a local path, or a PIL image.\"\n        )\n    image = PIL.ImageOps.exif_transpose(image)\n    image = image.convert(\"RGB\")\n    return image\n\n\n@requires(backends=(\"torchvision\",))\ndef load_image_as_tensor(\n    image: Union[str, \"PIL.Image.Image\"],\n    timeout: float | None = None,\n) -> \"torch.Tensor\":\n    \"\"\"\n    Loads `image` directly to a `torch.Tensor` using torchvision.\n\n    Args:\n        image (`str` or `PIL.Image.Image`):\n            The image to convert to the PIL Image format.","sourceCodeStart":489,"sourceCodeEnd":525,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/image_utils.py#L489-L525","documentation":"Raised as TypeError by `transformers.image_utils.load_image` when the input is not a string and not a PIL.Image.Image — i.e. the else-branch of the type dispatch. `load_image` accepts exactly three things: URL/path/base64 strings, and PIL images. Numpy arrays, torch tensors, bytes, and None are all rejected with this message.","triggerScenarios":"`load_image(np_array)`, `load_image(torch_tensor)`, `load_image(raw_bytes)`, or `load_image(None)` (e.g. from a failed fetch upstream). List inputs are also rejected here — use `load_images` for those.","commonSituations":"Assuming load_image is a universal converter and feeding it already-decoded arrays; bytes downloaded manually before calling; reusing the same variable for paths and loaded images in mixed pipelines.","solutions":["For numpy/torch inputs, skip loading — they are already valid image inputs for processors.","Wrap raw bytes: `PIL.Image.open(BytesIO(raw))`.","For lists of images, call `load_images(...)` instead.","Ensure upstream fetches cannot pass None silently."],"exampleFix":"// before\nimg = load_image(np.array(pil_img))   # TypeError\n\n// after\nimg = pil_img                          # pass PIL directly to load_image, or:\ninputs = processor(images=np.array(pil_img), return_tensors=\"pt\")","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"def is_load_image_input(x) -> bool:\n    import PIL\n    return isinstance(x, str) or isinstance(x, PIL.Image.Image)","tryCatchPattern":null,"preventionTips":["Reserve load_image for strings and PIL images; arrays/tensors already pass to processors directly.","Convert bytes with PIL.Image.open(BytesIO(b)) before use.","Use load_images for lists of sources."],"tags":["image-processing","type-checking","loading"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}