module documentation

Image utilities.

Copyright 2017-2025, Voxel51, Inc.

Function read Reads the image from the given path as a numpy array.
Function reencode_image Re-encodes the image to the format specified by the given output path.
Function reencode_images Re-encodes the images in the sample collection to the given format.
Function transform_image Transforms the image according to the provided parameters.
Function transform_images Transforms the images in the sample collection according to the provided parameters.
Function write Writes image to file.
Variable logger Undocumented
Function _do_transform Undocumented
Function _get_outpath Undocumented
Function _parse_ext Undocumented
Function _parse_parameters Undocumented
Function _transform_image Undocumented
Function _transform_images Undocumented
Function _transform_images_multi Undocumented
Function _transform_images_single Undocumented
def read(path_or_url, include_alpha=False, flag=None): (source)

Reads the image from the given path as a numpy array.

Color images are returned as RGB arrays.

Parameters
path_or_urlUndocumented
include_alpha:Falsewhether to include the alpha channel of the image, if present, in the returned array
flag:Nonean optional OpenCV image format flag to use. If provided, this flag takes precedence over include_alpha
paththe filepath or URL of the image
Returns
a uint8 numpy array containing the image
def reencode_image(input_path, output_path): (source)

Re-encodes the image to the format specified by the given output path.

Parameters
input_paththe path to the input image
output_paththe path to write the output image
def reencode_images(sample_collection, ext='.png', force_reencode=True, media_field='filepath', output_field=None, output_dir=None, rel_dir=None, update_filepaths=True, delete_originals=False, num_workers=None, skip_failures=False, progress=None): (source)

Re-encodes the images in the sample collection to the given format.

If no output_dir is specified and delete_originals is False, then if a transformation would result in overwriting an existing file with the same filename, the original file is renamed to <name>-original.<ext>.

Note

This method will not update the metadata field of the collection after transforming. You can repopulate the metadata field if needed by calling:

sample_collection.compute_metadata(overwrite=True)
Parameters
sample_collectiona fiftyone.core.collections.SampleCollection
ext:".png"the image format to use (e.g., ".png" or ".jpg")
force_reencode:Truewhether to re-encode images whose extension already matches ext
media_field:"filepath"the input field containing the image paths to transform
output_field:Nonean optional field in which to store the paths to the transformed images. By default, media_field is updated in-place
output_dir:Nonean optional output directory in which to write the transformed images. If none is provided, the images are updated in-place
rel_dir:Nonean optional relative directory to strip from each input filepath to generate a unique identifier that is joined with output_dir to generate an output path for each image. This argument allows for populating nested subdirectories in output_dir that match the shape of the input paths
update_filepaths:Truewhether to store the output paths on the sample collection
delete_originals:Falsewhether to delete the original images after re-encoding
num_workers:Nonea suggested number of worker processes to use
skip_failures:Falsewhether to gracefully continue without raising an error if an image cannot be re-encoded
progress:Nonewhether to render a progress bar (True/False), use the default value fiftyone.config.show_progress_bars (None), or a progress callback function to invoke instead
def transform_image(input_path, output_path, size=None, min_size=None, max_size=None, interpolation=None): (source)

Transforms the image according to the provided parameters.

Parameters
input_paththe path to the input image
output_paththe path to write the output image
size:Nonean optional (width, height) for the image. One dimension can be -1, in which case the aspect ratio is preserved
min_size:Nonean optional minimum (width, height) for the image. A dimension can be -1 if no constraint should be applied. The image is resized (aspect-preserving) if necessary to meet this constraint
max_size:Nonean optional maximum (width, height) for the image. A dimension can be -1 if no constraint should be applied. The image is resized (aspect-preserving) if necessary to meet this constraint
interpolation:Nonean optional interpolation argument for cv2.resize()
def transform_images(sample_collection, size=None, min_size=None, max_size=None, interpolation=None, ext=None, force_reencode=False, media_field='filepath', output_field=None, output_dir=None, rel_dir=None, update_filepaths=True, delete_originals=False, num_workers=None, skip_failures=False, progress=None): (source)

Transforms the images in the sample collection according to the provided parameters.

If no output_dir is specified and delete_originals is False, then if a transformation would result in overwriting an existing file with the same filename, the original file is renamed to <name>-original.<ext>.

Note

This method will not update the metadata field of the collection after transforming. You can repopulate the metadata field if needed by calling:

sample_collection.compute_metadata(overwrite=True)
Parameters
sample_collectiona fiftyone.core.collections.SampleCollection
size:Nonean optional (width, height) for each image. One dimension can be -1, in which case the aspect ratio is preserved
min_size:Nonean optional minimum (width, height) for each image. A dimension can be -1 if no constraint should be applied. The images are resized (aspect-preserving) if necessary to meet this constraint
max_size:Nonean optional maximum (width, height) for each image. A dimension can be -1 if no constraint should be applied. The images are resized (aspect-preserving) if necessary to meet this constraint
interpolation:Nonean optional interpolation argument for cv2.resize()
ext:Nonean optional image format to re-encode the source images into (e.g., ".png" or ".jpg")
force_reencode:Falsewhether to re-encode images whose parameters already match the specified values
media_field:"filepath"the input field containing the image paths to transform
output_field:Nonean optional field in which to store the paths to the transformed images. By default, media_field is updated in-place
output_dir:Nonean optional output directory in which to write the transformed images. If none is provided, the images are updated in-place
rel_dir:Nonean optional relative directory to strip from each input filepath to generate a unique identifier that is joined with output_dir to generate an output path for each image. This argument allows for populating nested subdirectories in output_dir that match the shape of the input paths
update_filepaths:Truewhether to store the output paths on the sample collection
delete_originals:Falsewhether to delete the original images if any transformation was applied
num_workers:Nonea suggested number of worker processes to use
skip_failures:Falsewhether to gracefully continue without raising an error if an image cannot be transformed
progress:Nonewhether to render a progress bar (True/False), use the default value fiftyone.config.show_progress_bars (None), or a progress callback function to invoke instead
def write(img, path): (source)

Writes image to file.

Parameters
imga numpy array
paththe output path

Undocumented

def _do_transform(args): (source)

Undocumented

def _get_outpath(inpath, output_dir=None, rel_dir=None): (source)

Undocumented

def _parse_ext(ext): (source)

Undocumented

def _parse_parameters(img, size, min_size, max_size): (source)

Undocumented

def _transform_image(inpath, outpath, size=None, min_size=None, max_size=None, interpolation=None, force_reencode=False, delete_original=False, skip_failures=False): (source)

Undocumented

def _transform_images(sample_collection, size=None, min_size=None, max_size=None, interpolation=None, ext=None, force_reencode=False, media_field='filepath', output_field=None, output_dir=None, rel_dir=None, update_filepaths=True, delete_originals=False, num_workers=None, skip_failures=False, progress=None): (source)

Undocumented

def _transform_images_multi(sample_collection, num_workers, size=None, min_size=None, max_size=None, interpolation=None, ext=None, force_reencode=False, media_field='filepath', output_field=None, output_dir=None, rel_dir=None, update_filepaths=True, delete_originals=False, skip_failures=False, progress=None): (source)

Undocumented

def _transform_images_single(sample_collection, size=None, min_size=None, max_size=None, interpolation=None, ext=None, force_reencode=False, media_field='filepath', output_field=None, output_dir=None, rel_dir=None, update_filepaths=True, delete_originals=False, skip_failures=False, progress=None): (source)

Undocumented