Image Utils¶
Crops the given image based on the given bounding box.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ImageType
|
The image to be cropped. |
required |
|
Union[ndarray, List[int], Tuple[int, int, int, int]]
|
A bounding box
coordinates in the format |
required |
Returns:
Type | Description |
---|---|
ImageType
|
The cropped image. The type is determined by the input type and
may be either a |
Source code in supervision/utils/image.py
Scales the given image based on the given scale factor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ImageType
|
The image to be scaled. |
required |
|
float
|
The factor by which the image will be scaled. Scale
factor > |
required |
Returns:
Type | Description |
---|---|
ImageType
|
The scaled image. The type is determined by the input type and
may be either a |
Raises:
Type | Description |
---|---|
ValueError
|
If the scale factor is non-positive. |
Source code in supervision/utils/image.py
Resizes the given image to a specified resolution. Can maintain the original aspect ratio or resize directly to the desired dimensions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ImageType
|
The image to be resized. |
required |
|
Tuple[int, int]
|
The target resolution as
|
required |
|
bool
|
Flag to maintain the image's original
aspect ratio. Defaults to |
False
|
Returns:
Type | Description |
---|---|
ImageType
|
The resized image. The type is determined by the input type and
may be either a |
Source code in supervision/utils/image.py
Resizes and pads an image to a specified resolution with a given color, maintaining the original aspect ratio.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
ImageType
|
The image to be resized. |
required |
|
Tuple[int, int]
|
The target resolution as
|
required |
|
Union[Tuple[int, int, int], Color]
|
The color to pad with. If tuple provided it should be in BGR format. |
BLACK
|
Returns:
Type | Description |
---|---|
ImageType
|
The resized image. The type is determined by the input type and
may be either a |
Source code in supervision/utils/image.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
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 |
---|---|---|---|
|
ndarray
|
The background scene onto which the image is placed. |
required |
|
ndarray
|
The image to be placed onto the scene. |
required |
|
Tuple[int, int]
|
The |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The result image with overlay. |
Examples:
import cv2
import numpy as np
import supervision as sv
image = cv2.imread(<SOURCE_IMAGE_PATH>)
overlay = np.zeros((400, 400, 3), dtype=np.uint8)
result_image = sv.overlay_image(image=image, overlay=overlay, anchor=(200, 400))
Source code in supervision/utils/image.py
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 |
---|---|---|---|
|
str
|
The target directory where images will be saved. |
required |
|
bool
|
Whether to overwrite the existing directory. Defaults to False. |
False
|
|
str
|
The image file name pattern. Defaults to "image_{:05d}.png". |
'image_{:05d}.png'
|
Examples:
import supervision as sv
frames_generator = sv.get_video_frames_generator(<SOURCE_VIDEO_PATH>, stride=2)
with sv.ImageSink(target_dir_path=<TARGET_CROPS_DIRECTORY>) as sink:
for image in frames_generator:
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 |
---|---|---|---|
|
ndarray
|
The image to be saved. The image must be in BGR color format. |
required |
|
Optional[str]
|
The name to use for the saved image.
If not provided, a name will be
generated using the |
None
|