pyclickimage.annotation_session#

class pyclickimage.annotation_session.AnnotationSession[source]#

Bases: object

Manage a multi-image annotation session.

An AnnotationSession stores the complete state of an annotation project, including loaded images, annotation groups, current selections, and output formatting options.

Each image is represented by an ImageEntry containing:

  • the image file path,

  • the associated ClickManager.

Parameters:

None

images#

Images included in the current session.

Type:

list[ImageEntry]

groups#

Names of annotation groups available in the session.

Type:

list[str]

current_image_index#

Index of the currently selected image. -1 means that no image is selected.

Type:

int

current_group_index#

Index of the currently selected annotation group. -1 means that no group is selected.

Type:

int or None

precision_mode#

Output coordinate precision mode.

Type:

PrecisionMode

Notes

The session object manages the relationship between images and groups. ClickManager objects only store annotation data and do not manage global session state.

_create_click_manager() → ClickManager[source]#

Create a click manager initialized with all session groups.

Returns:

Newly created click manager.

Return type:

ClickManager

property _current_annotation: tuple[ClickManager, str] | None#

Return the current annotation context.

Returns:

The current click manager and selected group, or None if no image or no group is currently selected.

Return type:

tuple[ClickManager, str] or None

_current_group_index: int#
_current_image_index: int#
_find_group(group_name: str) → str[source]#

Return a group.

Parameters:

group_name (str) – Name of the group.

Returns:

Matching group.

Return type:

str

Raises:

KeyError – If the group does not exist.

_find_group_index(group_name: str) → int[source]#

Return the index of a group.

Parameters:

group_name (str) – Name of the group.

Returns:

Index of the group.

Return type:

int

Raises:

KeyError – If the group does not exist.

_find_image(image_path: str | Path) → ImageEntry[source]#

Return an image entry.

Parameters:

image_path (str or pathlib.Path) – Image path to search.

Returns:

Matching image entry.

Return type:

ImageEntry

Raises:

KeyError – If the image does not exist.

_find_image_index(image_path: str | Path) → int[source]#

Return the index of an image.

Parameters:

image_path (str or pathlib.Path) – Image path to search.

Returns:

Index of the image in the session.

Return type:

int

Raises:

KeyError – If the image does not exist.

static _natural_group_sorting_key(group: str) → list[source]#

Return a natural sorting key for a group.

Parameters:

group (str) – Group name.

Returns:

Natural sorting key.

Return type:

list

static _natural_path_sorting_key(path: Path, fullpath: bool = True) → list[source]#

Return a natural sorting key for an image path.

Parameters:
  • path (pathlib.Path) – Image path.

  • fullpath (bool, default=True) – If True, use the full path. Otherwise, use only the filename.

Returns:

Natural sorting key.

Return type:

list

_restore_current_group(current_group: str | None) → None[source]#

Restore the current group selection after reordering.

Parameters:

current_group (str or None) – Previously selected group.

_restore_current_image(current_image: ImageEntry | None) → None[source]#

Restore the current image selection after reordering.

Parameters:

current_image (ImageEntry or None) – Previously selected image.

Notes

The selection is restored by object identity, allowing the image list to be reordered without losing the current selection.

_sort_groups(*, key) → None[source]#

Sort groups while preserving the current selection.

Parameters:

key (callable) – Sorting key passed to list.sort().

_sort_images(*, key) → None[source]#

Sort images while preserving the current selection.

Parameters:

key (callable) – Sorting key passed to list.sort().

add_click(x: int | float, y: int | float) → None[source]#

Add a click to the current annotation group.

Parameters:
  • x (numbers.Number) – Horizontal coordinate.

  • y (numbers.Number) – Vertical coordinate.

Notes

Does nothing if no image or group is selected.

add_group(group_name: str) → None[source]#

Add a new annotation group.

The group is created in every existing ClickManager.

Parameters:

group_name (str) – Group name.

Raises:
  • TypeError – If group_name is not a string.

  • ValueError – If the group already exists.

add_image(image_path: str | Path) → None[source]#

Add an image to the session.

A dedicated ClickManager is automatically created for the image and initialized with every existing annotation group.

Parameters:

image_path (str or pathlib.Path) – Image path.

Raises:

ValueError – If the path contains a comma.

apply_half_shift(mode: Literal['on', 'off']) → None[source]#

Enable or disable half-shift mode for every image.

Parameters:

mode ({"on", "off"}) – Half-shift mode to apply to every ClickManager.

Raises:

ValueError – If mode is invalid.

property current_click_manager: ClickManager | None#

Return the click manager associated with the current image.

Returns:

Click manager of the selected image, or None if no image is selected.

Return type:

ClickManager or None

property current_clicks: List[Tuple[int | float | None, int | float | None]]#

Return the clicks of the current annotation group.

Returns:

Clicks of the current image and group.

Return type:

Group

Notes

Returns an empty group if no image or group is selected.

property current_group: str | None#

Return the currently selected annotation group.

Returns:

Current group, or None if no group is selected.

Return type:

str or None

property current_group_index: int#

Return the index of the currently selected group.

Returns:

Current group index, or -1 if no group is selected.

Return type:

int

property current_image: ImageEntry | None#

Return the currently selected image.

Returns:

Selected image, or None if no image is selected.

Return type:

ImageEntry or None

property current_image_index: int#

Return the currently selected image.

Returns:

Current image entry index, or -1 if no image is selected.

Return type:

ImageEntry or None

property current_image_path: Path | None#

Return the path of the currently selected image.

Returns:

Path of the selected image, or None if no image is selected.

Return type:

pathlib.Path or None

delete_group(group_name: str) → None[source]#

Delete an annotation group.

The group and all associated clicks are removed from every image.

Parameters:

group_name (str) – Group name.

classmethod from_csv(path: str) → AnnotationSession[source]#

Load a session from CSV.

Parameters:

path (str) – CSV file path.

Returns:

Loaded annotation session.

Return type:

AnnotationSession

Raises:

ValueError – If the CSV format is invalid.

classmethod from_file(path: str | Path) → AnnotationSession[source]#

Load an annotation session from a supported file format.

Supported formats:

  • JSON

  • CSV

classmethod from_json(path: str | Path) → AnnotationSession[source]#

Load an annotation session from JSON.

Parameters:

path (str or pathlib.Path) – JSON file path.

Returns:

Loaded session.

Return type:

AnnotationSession

Raises:

ValueError – If the JSON structure is invalid.

get_clicks(group_name: str) → List[Tuple[int | float | None, int | float | None]][source]#

Return the clicks of the group.

Parameters:

group_name (str) – Group name.

Returns:

Clicks of the current image and selected group.

Return type:

Group

Notes

Returns an empty group if no image is selected.

groups: List[str]#
images: List[ImageEntry]#
precision_mode: Literal['float', 'int']#
remove_all_clicks() → None[source]#

Remove all clicks from the current annotation group.

Notes

Does nothing if no image or group is selected.

remove_image(image_path: str | Path) → None[source]#

Remove an image from the session.

Parameters:

image_path (str or pathlib.Path) – Image to remove.

Raises:

KeyError – If the image does not exist.

remove_last_click() → None[source]#

Remove the last click from the current annotation group.

Notes

Does nothing if no image or group is selected.

rename_group(old_name: str, new_name: str) → None[source]#

Rename an annotation group.

Parameters:
  • old_name (str) – Current group name.

  • new_name (str) – New group name.

Raises:
  • KeyError – If old_name does not exist.

  • ValueError – If new_name already exists.

select_current_group(group_name: str | None) → None[source]#

Select the current annotation group.

Parameters:

group_name (str or None) – Group to select. If None, clears the current selection.

Raises:

KeyError – If the group does not exist.

select_current_group_index(index: int) → None[source]#

Select the current annotation group by index.

Parameters:

index (int) – Group index. Use -1 to clear the selection.

Raises:
  • TypeError – If index is not an integer.

  • IndexError – If the index is invalid.

select_current_image_index(index: int) → None[source]#

Select the current image by index.

Parameters:

index (int) – Image index. Use -1 to clear the selection.

Raises:
  • TypeError – If index is not an integer.

  • IndexError – If the index is out of range.

select_current_image_path(image_path: str | Path | None) → None[source]#

Select the current image.

Parameters:

image_path (str, pathlib.Path or None) – Image to select. If None, the current selection is cleared.

Raises:

KeyError – If the image does not exist.

set_precision_mode(precision_mode: Literal['float', 'int']) → None[source]#

Change output precision mode.

The internal coordinates remain stored as float. This only affects returned values and CSV export.

Parameters:

precision_mode ({"float", "int"}) – Output precision mode.

Raises:

ValueError – If the precision mode is invalid.

sort_groups_alpha() → None[source]#

Sort groups alphabetically.

Notes

The current group selection is preserved.

sort_groups_natural() → None[source]#

Sort groups using natural ordering.

Notes

The current group selection is preserved.

sort_images_alpha(fullpath: bool = True) → None[source]#

Sort images alphabetically.

Parameters:

fullpath (bool, default=True) – If True, sort using the complete path. Otherwise, sort using only the filename.

Notes

The current image selection is preserved.

sort_images_natural(fullpath: bool = True) → None[source]#

Sort images using natural ordering.

Parameters:

fullpath (bool, default=True) – If True, sort using the complete path. Otherwise, sort using only the filename.

Notes

The current image selection is preserved.

to_csv(path: str) → None[source]#

Save the complete session to CSV.

Parameters:
  • path (str) – Output CSV file path.

  • Format

  • ------

  • Image

  • Group

  • Index

  • X

  • Y

to_json(path: str | Path) → None[source]#

Save the complete annotation session to JSON.

Parameters:

path (str or pathlib.Path) – Output JSON file path.

class pyclickimage.annotation_session.ImageEntry(path: Path, click_manager: ClickManager)[source]#

Bases: object

Store an image and its associated annotations.

An ImageEntry links an image file path with the ClickManager responsible for storing its annotation points.

Parameters:
  • path (pathlib.Path) – Path to the image file.

  • click_manager (ClickManager) – Manager storing annotation clicks associated with this image.

Notes

Each image in an AnnotationSession owns a dedicated ClickManager. This avoids maintaining separate synchronized containers for images and click managers.

click_manager: ClickManager#
path: Path#