Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/transformers/image_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from dataclasses import dataclass
from io import BytesIO
from typing import Optional, Union
from pathlib import Path

import httpx
import numpy as np
Expand Down Expand Up @@ -439,12 +440,12 @@ def valid_coco_panoptic_annotations(annotations: Iterable[dict[str, list | tuple
return all(is_valid_annotation_coco_panoptic(ann) for ann in annotations)


def load_image(image: Union[str, "PIL.Image.Image"], timeout: float | None = None) -> "PIL.Image.Image":
def load_image(image: Union[str, Path, "PIL.Image.Image"], timeout: float | None = None) -> "PIL.Image.Image":
"""
Loads `image` to a PIL Image.

Args:
image (`str` or `PIL.Image.Image`):
image (`str`, `Path` or `PIL.Image.Image`):
The image to convert to the PIL Image format.
timeout (`float`, *optional*):
The timeout value in seconds for the URL request.
Expand All @@ -453,6 +454,11 @@ def load_image(image: Union[str, "PIL.Image.Image"], timeout: float | None = Non
`PIL.Image.Image`: A PIL Image.
"""
requires_backends(load_image, ["vision"])

# Convert Path to string
Comment thread
Rocketknight1 marked this conversation as resolved.
Resolved
if isinstance(image, Path):
image = str(image)

if isinstance(image, str):
if image.startswith("http://") or image.startswith("https://"):
# We need to actually check for a real protocol, otherwise it's impossible to use a local file
Expand Down