Image
ImageSink¶
Source code in supervision/utils/image.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
|
__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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
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
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
|
crop¶
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=image)
Source code in supervision/utils/image.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
|