Utility functions#

Utility functions, e.g. to load and display images ,e.g. pp.utils.load_image() and pp.utils.show_image(). Any of these functions can be called directly after loading phenopype without the need to add the utils submodule:

import phenopype as pp

image_path = "image_dir/my_image.jpg"

image = pp.load_image(image_path)       # instead of pp.utils.load_image
pp.show_image(image)                    # instead of pp.utils.show_image
dir(pp)                                 # shows available classes and functions
class phenopype.utils.Container(image, dir_path, **kwargs)#

Bases: object

A phenopype container is a Python class where loaded images, dataframes, detected contours, intermediate output, etc. are stored so that they are available for inspection or storage at the end of the analysis. The advantage of using containers is that they don’t litter the global environment and namespace, while still containing all intermediate steps (e.g. binary masks or contour DataFrames). Containers can be used manually to analyse images, but typically they are created dynamically within the pype-routine.

Parameters:
  • image_path (ndarray) – path to an image to build the container from. Image directory will be used as the working-directory to store all Pype-output in

  • version (str, optional) – suffix to append to filename of results files. The default is “v1”.

load(contours=False, **kwargs)#

Autoload function for container: loads results files with given file_suffix into the container. Can be used manually, but is typically used within the pype routine.

Parameters:

file_suffix (str, optional) – suffix to include when looking for files to load

reset()#

Resets modified images, canvas and df_image_data to original state. Can be used manually, but is typically used within the pype routine.

run(fun, fun_kwargs={}, annotation_kwargs={}, annotation_counter={})#
save(dir_path=None, export_list=[], overwrite=False, **kwargs)#

Autosave function for container.

Parameters:
  • dir_path (str, optional) – provide a custom directory where files should be save - overwrites dir_path provided from container, if applicable

  • export_list (list, optional) – used in pype rountine to check against already performed saving operations. running container.save() with an empty export_list will assumed that nothing has been saved so far, and will try

  • overwrite (bool, optional) – gloabl overwrite flag in case file exists

phenopype.utils.load_image(path, mode='unchanged', **kwargs)#

Create ndarray from image path or return or resize exising array.

Parameters:
  • path (str) – path to an image stored on the harddrive

  • mode ({"default", "colour","gray"} str, optional) –

    image conversion on loading:
    • default: load image as is

    • colour: convert image to 3-channel (BGR)

    • gray: convert image to single channel (grayscale)

  • kwargs – developer options

Returns:

  • container (container) – A phenopype container is a Python class where loaded images, dataframes, detected contours, intermediate output, etc. are stored so that they are available for inspection or storage at the end of the analysis.

  • image (ndarray) – original image (resized, if selected)

phenopype.utils.load_template(template_path, tag='v1', overwrite=False, keep_comments=True, image_path=None, dir_path=None, ret_path=False)#
Parameters:
  • template_path (TYPE) – DESCRIPTION.

  • tag (TYPE, optional) – DESCRIPTION. The default is “v1”.

  • overwrite (TYPE, optional) – DESCRIPTION. The default is False.

  • keep_comments (TYPE, optional) – DESCRIPTION. The default is True.

  • image_path (TYPE, optional) – DESCRIPTION. The default is None.

  • dir_path (TYPE, optional) – DESCRIPTION. The default is None.

  • ret_path (TYPE, optional) – DESCRIPTION. The default is False.

Returns:

config_path – DESCRIPTION.

Return type:

TYPE

phenopype.utils.print_colours()#
phenopype.utils.save_image(image, file_name, dir_path, resize=1, ext='jpg', overwrite=False, **kwargs)#

Save an image (array) to jpg.

Parameters:
  • image (array) – image to save

  • name (str) – name for saved image

  • save_dir (str, optional) – directory to save image

  • append (str, optional) – append image name with string to prevent overwriting

  • extension (str, optional) – file extension to save image as

  • overwrite (boo, optional) – overwrite images if name exists

  • resize (float, optional) – resize factor for the image (1 = 100%, 0.5 = 50%, 0.1 = 10% of original size).

  • kwargs – developer options

phenopype.utils.show_image(image, return_input=False, position_reset=True, position_offset=25, window_aspect='normal', check=True, **kwargs)#

Show one or multiple images by providing path string or array or list of either.

Parameters:
  • image (array, list of arrays) – the image or list of images to be displayed. can be array-type, or list or arrays

  • window_max_dim (int, optional) – maximum dimension on either acis

  • window_aspect ({"fixed", "free"} str, optional) – type of opencv window (“free” is resizeable)

  • position_reset (bool, optional) – flag whether image positions should be reset when reopening list of images

  • position_offset (int, optional) – if image is list, the distance in pixels betweeen the positions of each newly opened window (only works in conjunction with “position_reset”)

  • check (bool, optional) – user input required when more than 10 images are opened at the same time