Image Utils¶
supervision.utils.image.crop_image(image: ImageType, xyxy: npt.NDArray[np.number] | list[int] | tuple[int, int, int, int]) -> ImageType
¶
Crop image based on bounding box coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ImageType
|
The image to crop. |
required |
|
NDArray[number] | list[int] | tuple[int, int, int, int]
|
Bounding box coordinates in |
required |
Returns:
| Type | Description |
|---|---|
ImageType
|
Cropped image matching input type. |
Note
Coordinates are rounded to integers and clipped to the image bounds before slicing. This keeps NumPy and Pillow inputs aligned and avoids negative-index wrap-around on NumPy arrays. A box with no pixels inside the image (outside it, or with no area) returns an empty image of the input type.
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> image = np.zeros((1080, 1920, 3), dtype=np.uint8)
>>> image.shape
(1080, 1920, 3)
>>> xyxy = (400, 400, 800, 800)
>>> cropped_image = sv.crop_image(image=image, xyxy=xyxy)
>>> cropped_image.shape
(400, 400, 3)
>>> image = np.zeros((1920, 1080), dtype=np.uint8)
>>> image.shape
(1920, 1080)
>>> xyxy = (400, 400, 800, 800)
>>> cropped_image = sv.crop_image(image=image, xyxy=xyxy)
>>> cropped_image.shape
(400, 400)

Source code in src/supervision/utils/image.py
supervision.utils.image.load_image_from_url(value: str, cv_imread_flags: int = cv2.IMREAD_COLOR, timeout: float = 30.0, use_cache: bool = True, cache_dir: str | Path | None = None, force_reload: bool = False) -> npt.NDArray[np.uint8]
¶
Load an image from a URL as an OpenCV image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
HTTP(S) URL of the image. |
required |
|
int
|
OpenCV image read flag passed to |
IMREAD_COLOR
|
|
float
|
Request timeout in seconds. Defaults to |
30.0
|
|
bool
|
If |
True
|
|
str | Path | None
|
Directory where downloaded image bytes are cached. If |
None
|
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
NDArray[uint8]
|
Image as a NumPy array in the format selected by |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the URL is invalid. |
ValueError
|
If the downloaded bytes cannot be decoded. |
RequestException
|
If the request fails or returns an error status. |
Note
Without OpenCV, cv_imread_flags=0 (IMREAD_GRAYSCALE) returns a
two-dimensional uint8 image. Pillow-backed grayscale conversions for
other codecs or color modes can differ from OpenCV's decoder, even when
their shape and dtype agree.
Examples:
import supervision as sv
image = sv.load_image_from_url(
"https://media.roboflow.com/quickstart/dog.jpeg"
)
image.shape
Source code in src/supervision/utils/image.py
supervision.utils.image.scale_image(image: ImageType, scale_factor: float) -> ImageType
¶
Scale image by given factor. Scale factor > 1.0 zooms in, < 1.0 zooms out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ImageType
|
The image to scale. |
required |
|
float
|
Factor by which to scale the image. |
required |
Returns:
| Type | Description |
|---|---|
ImageType
|
Scaled image matching input type. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If scale factor is non-positive. |
Note
Each axis is truncated to at least one pixel. A fractional binary64 product immediately below an integer is corrected to that integer. A uint8 BGRA image whose alpha varies is interpolated with alpha-weighted colors, so the color hidden under a transparent pixel does not bleed into its visible neighbors.
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> image = np.zeros((1080, 1920, 3), dtype=np.uint8)
>>> image.shape
(1080, 1920, 3)
>>> scaled_image = sv.scale_image(image=image, scale_factor=0.5)
>>> scaled_image.shape
(540, 960, 3)
>>> image = np.zeros((1920, 1080), dtype=np.uint8)
>>> image.shape
(1920, 1080)
>>> scaled_image = sv.scale_image(image=image, scale_factor=0.5)
>>> scaled_image.shape
(960, 540)

Source code in src/supervision/utils/image.py
supervision.utils.image.resize_image(image: ImageType, resolution_wh: tuple[int, int], keep_aspect_ratio: bool = False) -> ImageType
¶
Resize image to specified resolution. Can optionally maintain aspect ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ImageType
|
The image to resize. |
required |
|
tuple[int, int]
|
Target resolution as |
required |
|
bool
|
Flag to maintain original aspect ratio.
Defaults to |
False
|
Returns:
| Type | Description |
|---|---|
ImageType
|
Resized image matching input type. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Note
With keep_aspect_ratio=True the fitted axis is rounded down to a whole
pixel and keeps at least one pixel, so an aspect ratio too extreme to fit
the target box returns a one-pixel-wide or one-pixel-tall image rather
than raising. A target with the same aspect ratio as the image returns
exactly the target size. A uint8 BGRA image whose alpha varies is
interpolated with alpha-weighted colors, so the color hidden under a
transparent pixel does not bleed into its visible neighbors.
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> image = np.zeros((1080, 1920, 3), dtype=np.uint8)
>>> image.shape
(1080, 1920, 3)
>>> resized_image = sv.resize_image(
... image=image, resolution_wh=(1000, 1000), keep_aspect_ratio=True
... )
>>> resized_image.shape
(562, 1000, 3)
>>> image = np.zeros((1920, 1080), dtype=np.uint8)
>>> image.shape
(1920, 1080)
>>> resized_image = sv.resize_image(
... image=image, resolution_wh=(1000, 1000), keep_aspect_ratio=True
... )
>>> resized_image.shape
(1000, 562)

Source code in src/supervision/utils/image.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |
supervision.utils.image.letterbox_image(image: ImageType, resolution_wh: tuple[int, int], color: tuple[int, int, int] | Color = Color.BLACK) -> ImageType
¶
Resize image and pad with color to achieve desired resolution while maintaining aspect ratio.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ImageType
|
The image to resize and pad. Accepts BGR arrays of shape
|
required |
|
tuple[int, int]
|
Target resolution as |
required |
|
tuple[int, int, int] | Color
|
Padding color. If tuple, should be in BGR format.
Defaults to :attr: |
BLACK
|
Returns:
| Type | Description |
|---|---|
ImageType
|
Letterboxed image matching input type. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
Note
For BGRA inputs, the alpha channel in the padding region is set to
0 (fully transparent). Grayscale inputs receive scalar padding
from color[0].
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> image = np.zeros((1080, 1920, 3), dtype=np.uint8)
>>> image.shape
(1080, 1920, 3)
>>> letterboxed_image = sv.letterbox_image(
... image=image, resolution_wh=(1000, 1000)
... )
>>> letterboxed_image.shape
(1000, 1000, 3)
>>> gray = np.zeros((4, 6), dtype=np.uint8)
>>> sv.letterbox_image(image=gray, resolution_wh=(10, 10)).shape
(10, 10)

Source code in src/supervision/utils/image.py
supervision.utils.image.tint_image(image: ImageType, color: Color = Color.BLACK, opacity: float = 0.5) -> ImageType
¶
Tint image with solid color overlay at specified opacity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ImageType
|
The image to tint. Accepts BGR arrays of shape |
required |
|
Color
|
Overlay tint color. Defaults to :attr: |
BLACK
|
|
float
|
Blend ratio between overlay and image (0.0-1.0).
Defaults to |
0.5
|
Returns:
| Type | Description |
|---|---|
ImageType
|
Tinted image matching input
type. The input image is left unchanged. A grayscale input comes back
as a BGR array (or an RGB |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If opacity is outside range [0.0, 1.0]. |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> image = np.zeros((100, 100, 3), dtype=np.uint8)
>>> tinted_image = sv.tint_image(
... image=image, color=sv.Color.ROBOFLOW, opacity=0.5
... )
>>> tinted_image.shape
(100, 100, 3)
>>> int(image.max())
0

Source code in src/supervision/utils/image.py
supervision.utils.image.grayscale_image(image: ImageType) -> ImageType
¶
Convert image to 3-channel grayscale. Luminance channel is broadcast to all three channels for compatibility with color-based drawing helpers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ImageType
|
The image to convert to grayscale. Accepts BGR arrays of shape
|
required |
Returns:
| Type | Description |
|---|---|
ImageType
|
3-channel grayscale image matching input type. |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> image = np.ones((100, 100, 3), dtype=np.uint8) * 128
>>> grayscale_image = sv.grayscale_image(image=image)
>>> grayscale_image.shape
(100, 100, 3)

Source code in src/supervision/utils/image.py
supervision.utils.image.get_image_resolution_wh(image: ImageType) -> tuple[int, int]
¶
Get image width and height as a tuple (width, height) for various image
formats.
Supports both numpy.ndarray images (with shape (H, W, ...)) and
PIL.Image.Image inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
ImageType
|
Input image. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
Image resolution as |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a |
TypeError
|
If |
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> image = np.zeros((1080, 1920, 3), dtype=np.uint8)
>>> sv.get_image_resolution_wh(image)
(1920, 1080)
Source code in src/supervision/utils/image.py
supervision.utils.image.ImageSink
¶
Save sequential images into a directory through a context manager.
:class:~supervision.utils.image.ImageSink creates the target directory on
entry and writes each image using
:meth:~supervision.utils.image.ImageSink.save_image, incrementing the image
name pattern after every save.
Source code in src/supervision/utils/image.py
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 | |
Methods:¶
__init__(target_dir_path: str, overwrite: bool = False, image_name_pattern: str = 'image_{:05d}.png') -> None
¶
Initialize context manager for saving images to directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
Target directory path where images will be saved. |
required |
|
bool
|
Whether to overwrite existing directory.
Also permits replacing an existing image file. Defaults to |
False
|
|
str
|
File name pattern for saved images.
Defaults to |
'image_{:05d}.png'
|
Examples:
>>> import numpy as np
>>> import supervision as sv
>>> import tempfile
>>> import os
>>> with tempfile.TemporaryDirectory() as tmpdir:
... image = np.zeros((100, 100, 3), dtype=np.uint8)
... with sv.ImageSink(target_dir_path=tmpdir, overwrite=True) as sink:
... sink.save_image(image=image)
... sink.save_image(image=image)
... files = sorted(os.listdir(tmpdir))
... len(files)
2
Source code in src/supervision/utils/image.py
save_image(image: npt.NDArray[np.uint8], image_name: str | None = None) -> None
¶
Save image to target directory with optional custom filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
NDArray[uint8]
|
Image to save with shape |
required |
|
str | None
|
Custom filename for saved image. If
|
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the target path contains an embedded null character. |
FileExistsError
|
If |
OSError
|
If |