imgutils.data.url

This module provides utilities for downloading and handling images from URLs, with special support for GitHub and Hugging Face URLs.

The module includes functions for:

  • Downloading images from URLs with progress tracking

  • URL validation and processing

  • Special handling for GitHub and Hugging Face hosted images

Main components:

  • download_image_from_url: Downloads and returns an image from a given URL

  • is_http_url: Checks if a given URL is a valid HTTP/HTTPS URL

  • Internal utilities for processing GitHub and Hugging Face URLs

download_image_from_url

imgutils.data.url.download_image_from_url(url: str, silent: bool = False, expected_size: int | None = None, **kwargs) Image[source]

Download an image from a URL and return it as a PIL Image object.

Parameters:
  • url (str) – URL of the image to download

  • silent (bool) – If True, suppress progress bar display

  • expected_size (Optional[int]) – Expected file size in bytes, used for progress bar

  • kwargs – Additional keyword arguments passed to the session.get() method

Returns:

Downloaded image as PIL Image object

Return type:

Image.Image

Raises:
  • ValueError – If the URL is not supported (especially for HF URLs)

  • requests.RequestException – If download fails

  • PIL.UnidentifiedImageError – If downloaded content is not a valid image

Example:
>>> image = download_image_from_url('https://example.com/image.jpg')
>>> image.show()

is_http_url

imgutils.data.url.is_http_url(url: str) bool[source]

Check if a given URL is a valid HTTP or HTTPS URL.

Parameters:

url (str) – URL to check

Returns:

True if URL is a valid HTTP/HTTPS URL, False otherwise

Return type:

bool

Example:
>>> is_http_url('https://example.com')
True
>>> is_http_url('ftp://example.com')
False