write_roi#

ktch.io.write_roi(file_path, coords, roi_type='polygon', specimen_names=None, names=None, positions=None)[source]#

Write an ImageJ ROI file (.roi) or ROI set (.zip).

Writes the ROI types that store a list of coordinates, in the format ImageJ and Fiji read. Coordinates that are all integers are stored as integers; otherwise ImageJ’s sub-pixel coordinates are written too.

Parameters:
file_pathstr or pathlib.Path

A .roi path for one ROI, or a .zip path for a ROI set.

coordsarray-like, list of array-like, RoiData or list of RoiData

The coordinates of each ROI, in pixels with y downward (see RoiData). A 2-D array of shape (n_coords, 2) is one ROI; a 3-D array or a list holds one per ROI. A RoiData, such as read_roi() returns, carries its own type, names, stack position and flags; the other arguments must then be left out.

roi_typestr or list of str, default=”polygon”

"polygon", "freehand" or "traced" for a closed outline, "polyline" or "freeline" for an open curve, "angle" or "point". One value applies to every ROI.

specimen_namesstr or list of str, optional

The names of the entries of a ROI set, without .roi. They must be unique. Defaults to "0", "1", … A .roi file takes its name from file_path.

namesstr or list of str, optional

The names stored in the ROIs.

positionsint or list of int, optional

The positions in an image stack (1-based).

Raises:
ValueError

If file_path is not a .roi or .zip path, if a .roi path is given several ROIs, if the lengths of the arguments do not match, if entry names repeat, if a ROI has no coordinates, if a ROI other than a point ROI has more than 65535 coordinates, or if a coordinate lies outside the range ImageJ stores (-5000 to 60535).

See also

read_roi

Read an ImageJ ROI file or ROI set.

Examples

>>> import numpy as np
>>> from ktch.io import write_roi
>>> square = np.array([[10, 20], [14, 20], [14, 25], [10, 25]])
>>> write_roi("square.roi", square, names="square")