Image Utils¶
ImageSink
Source code in supervision/utils/image.py
Functions¶
__init__(target_dir_path, overwrite=False, image_name_pattern='image_{:05d}.png')
¶
Initialize a context manager for saving images.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target_dir_path |
str
|
The target directory where images will be saved. |
required |
overwrite |
bool
|
Whether to overwrite the existing directory. Defaults to False. |
False
|
image_name_pattern |
str
|
The image file name pattern. Defaults to "image_{:05d}.png". |
'image_{:05d}.png'
|
Examples:
import supervision as sv
with sv.ImageSink(target_dir_path='target/directory/path',
overwrite=True) as sink:
for image in sv.get_video_frames_generator(
source_path='source_video.mp4', stride=2):
sink.save_image(image=image)
Source code in supervision/utils/image.py
save_image(image, image_name=None)
¶
Save a given image in the target directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
ndarray
|
The image to be saved. |
required |
image_name |
str
|
The name to use for the saved image.
If not provided, a name will be
generated using the |
None
|
Source code in supervision/utils/image.py
crop_image
Crops the given image based on the given bounding box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
ndarray
|
The image to be cropped, represented as a numpy array. |
required |
xyxy |
ndarray
|
A numpy array containing the bounding box coordinates in the format (x1, y1, x2, y2). |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The cropped image as a numpy array. |
Examples:
import supervision as sv
detection = sv.Detections(...)
with sv.ImageSink(target_dir_path='target/directory/path') as sink:
for xyxy in detection.xyxy:
cropped_image = sv.crop_image(image=image, xyxy=xyxy)
sink.save_image(image=cropped_image)
Source code in supervision/utils/image.py
resize_image
Resizes an image by a given scale factor using cv2.INTER_LINEAR interpolation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
ndarray
|
The input image to be resized. |
required |
scale_factor |
float
|
The factor by which the image will be scaled. Scale factor
|
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The resized image. |
Raises:
Type | Description |
---|---|
ValueError
|
If the scale factor is non-positive. |
Source code in supervision/utils/image.py
place_image
Places an image onto a scene at a given anchor point, handling cases where the image's position is partially or completely outside the scene's bounds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scene |
ndarray
|
The background scene onto which the image is placed. |
required |
image |
ndarray
|
The image to be placed onto the scene. |
required |
anchor |
Tuple[int, int]
|
The (x, y) coordinates in the scene where the top-left corner of the image will be placed. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: The modified scene with the image placed at the anchor point, or unchanged if the image placement is completely outside the scene. |