original_dir ="src/images/original"path = os.path.join(original_dir, "lemons.jpg")meta ={"my-field-1":"my-value-1","my-field-2":"my-value-2"}image = api.image.upload_path( dataset.id, name="Lemons", path=path, meta=meta # optional)print(f'Image "{image.name}" uploaded to Supervisely with ID:{image.id}')
Output:
# Image "Lemons.jpeg" uploaded to Supervisely platform with ID:17539453
Upload list of images.
✅ Supervisely API allows uploading multiple images in a single request. The code sample below sends fewer requests and it leads to a significant speed-up of our original code.
Source code:
names = ["grapes-1.jpg","grapes-2.jpg","oranges-2.jpg","oranges-1.jpg",]paths = [os.path.join(original_dir, name)for name in names]upload_info = api.image.upload_paths(dataset.id, names, paths)print(f"{len(upload_info)} images successfully uploaded to Supervisely platform")
Output:
# 4 images successfully uploaded to Supervisely platform
Upload images as NumPy matrix
Single image
Source code:
img_np = sly.image.read(path)np_image_info = api.image.upload_np(dataset.id, name="Lemons-np.jpeg", img=img_np)print(f"Image successfully uploaded as NumPy matrix to Supervisely (ID: {np_image_info.id})")
Output:
# Image successfully uploaded as NumPy matrix to Supervisely (ID: 17539458)
Upload list of images
Source code:
names_np = [f"np-{name}"for name in names]images_np = [sly.image.read(img_path)for img_path in paths]np_images_info = api.image.upload_nps(dataset.id, names_np, images_np)print(f"{len(images_np)} images successfully uploaded to platform as NumPy matrix")
Output:
# 4 images successfully uploaded to platform as NumPy matrix
Get information about images
Single image
Get information about image from Supervisely by id.
You can also get information about image from Supervisely by name.
Source code:
image_name =get_file_name(image.name)image_info_by_name = api.image.get_info_by_name(dataset.id, image_name)print(f"image name - {image_info_by_name.name}")
Output:
# image name - Lemons.jpeg
Get all images from dataset.
Get information about image from Supervisely by id.
Source code:
image_info_list = api.image.get_list(dataset.id)print(f"{len(image_info_list)} images information received.")
Output:
# 10 images information received.
Download images to local directory
Single image
Download image from Supervisely to local directory by id.
Source code:
save_path = os.path.join(result_dir, image_info.name)api.image.download_path(image_info.id, save_path)print(f"Image has been successfully downloaded to '{save_path}'")
Output:
# Image has been successfully downloaded to 'src/images/result/Lemons.jpeg'
Download list of images to local directory
Download list of images from Supervisely to local directory by ids.
Source code:
image_ids = [img.id for img in image_info_list]image_names = [img.name for img in image_info_list]save_paths = [os.path.join(result_dir, img_name)for img_name in image_names]api.image.download_paths(dataset.id, image_ids, save_paths)print(f"{len(image_info_list)} images has been successfully downloaded.")
Output:
# 10 images has been successfully downloaded.
Download images as RGB NumPy matrix
Single image
Download image from Supervisely to local directory by id.
images_to_remove = api.image.get_list(dataset.id)remove_ids = [img.id for img in images_to_remove]api.image.remove_batch(remove_ids)print(f"{len(remove_ids)} images successfully removed.")
Output:
# 9 images successfully removed.
Custom image sorting for Image Labeling Toolbox
To enhance the usability of working with images in the Image Labeling Toolbox, a custom sorting parameter can be added for project images. This parameter will define the order of images in the interface list.
Sort button
Sorting parameter
Upload list of images with added custom sorting parameter
The best and fastest way to accomplish this is to use context manager ImageApi.add_custom_sort This context manager allows you to set the sort_by attribute of ImageApi object for the duration of the context, then delete it. If nested functions support this functionality, each image they process will automatically receive a custom sorting parameter based on the available meta object.
Currently, almost all image uploading methods support this functionality. Methods that support it have a corresponding description in the docstring.
Upload whole images project in Supervisely format with added custom sorting parameter
It is also recommended to use a context manager for uploading the entire project. The only difference from the previous point is that there is no need to pass meta in dictionaries. It can be stored either in image info files or in meta files within the project structure. To learn more about the project structure and its files, see the Project Structure section.
Source code:
from supervisely.project.upload import uploadproject_dir ="src/images_project"project_name ="Project with Sorting"with api.image.add_custom_sort(key="my-key"):upload(project_dir, api, workspace_id, project_name)project_info = api.project.get_info_by_name(workspace_id, project_name)dataset_info = api.dataset.get_list(project_info.id)[0]images_infos = api.image.get_list(dataset_info.id)for i in images_infos:print(f"{i.name}: {i.meta}")
Same as the previous case, but for more than one image
Source code:
image_ids = [image.id for image in images_infos]sort_values = ["1st","2nd","3rd","4th","5th"]api.image.set_custom_sort_bulk(image_ids, sort_values)images_infos = api.image.get_list(dataset_info.id)for i in images_infos:print(f"{i.name}: {i.meta}")