creatumlibre.graphics.selection.region_manager

 1import numpy as np
 2
 3
 4class RegionManager:
 5    """Handles selection modifications, mask updates, and dynamic resizing."""
 6
 7    def __init__(self):
 8        self.mask = None  # Stores selection mask (0-255)
 9        self.bounding_rect = None  # Stores current selection bounds
10
11    def copy(self):
12        new = RegionManager()
13        new.mask = self.mask.copy()
14        new.bounding_rect = self.bounding_rect
15        return new
16
17    def set_bounding_rect(self, x: int, y: int, width: int, height: int):
18        self.bounding_rect = (x, y, width, height)
19
20    def get_bounding_rect(self):
21        return self.bounding_rect
22
23    def get_mask(self):
24        return self.mask
25
26    def initialize_mask(self, image_shape):
27        """Creates an empty mask matching the image dimensions.
28        0: transparent, 1: opaque (visible)
29        """
30        self.mask = np.ones((image_shape[0], image_shape[1]), dtype=np.float32)
31
32    def update_mask(self, selection):
33        """Updates the mask based on selection coordinates."""
34        x, y, width, height = selection.get_rect()
35        self.mask[y : y + height, x : x + width] = (
36            1  # Mark selection area as fully opaque
37        )
38
39        # Update bounding rect
40        self.bounding_rect = (x, y, width, height)
41
42    def apply_mask(self, image, mask):
43        """Applies the mask onto the image, modifying selection visibility."""
44        masked_image = image.copy()
45        masked_image[mask == 0] = (0, 0, 0)  # Hide unselected regions
46
47        return masked_image
class RegionManager:
 5class RegionManager:
 6    """Handles selection modifications, mask updates, and dynamic resizing."""
 7
 8    def __init__(self):
 9        self.mask = None  # Stores selection mask (0-255)
10        self.bounding_rect = None  # Stores current selection bounds
11
12    def copy(self):
13        new = RegionManager()
14        new.mask = self.mask.copy()
15        new.bounding_rect = self.bounding_rect
16        return new
17
18    def set_bounding_rect(self, x: int, y: int, width: int, height: int):
19        self.bounding_rect = (x, y, width, height)
20
21    def get_bounding_rect(self):
22        return self.bounding_rect
23
24    def get_mask(self):
25        return self.mask
26
27    def initialize_mask(self, image_shape):
28        """Creates an empty mask matching the image dimensions.
29        0: transparent, 1: opaque (visible)
30        """
31        self.mask = np.ones((image_shape[0], image_shape[1]), dtype=np.float32)
32
33    def update_mask(self, selection):
34        """Updates the mask based on selection coordinates."""
35        x, y, width, height = selection.get_rect()
36        self.mask[y : y + height, x : x + width] = (
37            1  # Mark selection area as fully opaque
38        )
39
40        # Update bounding rect
41        self.bounding_rect = (x, y, width, height)
42
43    def apply_mask(self, image, mask):
44        """Applies the mask onto the image, modifying selection visibility."""
45        masked_image = image.copy()
46        masked_image[mask == 0] = (0, 0, 0)  # Hide unselected regions
47
48        return masked_image

Handles selection modifications, mask updates, and dynamic resizing.

mask
bounding_rect
def copy(self):
12    def copy(self):
13        new = RegionManager()
14        new.mask = self.mask.copy()
15        new.bounding_rect = self.bounding_rect
16        return new
def set_bounding_rect(self, x: int, y: int, width: int, height: int):
18    def set_bounding_rect(self, x: int, y: int, width: int, height: int):
19        self.bounding_rect = (x, y, width, height)
def get_bounding_rect(self):
21    def get_bounding_rect(self):
22        return self.bounding_rect
def get_mask(self):
24    def get_mask(self):
25        return self.mask
def initialize_mask(self, image_shape):
27    def initialize_mask(self, image_shape):
28        """Creates an empty mask matching the image dimensions.
29        0: transparent, 1: opaque (visible)
30        """
31        self.mask = np.ones((image_shape[0], image_shape[1]), dtype=np.float32)

Creates an empty mask matching the image dimensions. 0: transparent, 1: opaque (visible)

def update_mask(self, selection):
33    def update_mask(self, selection):
34        """Updates the mask based on selection coordinates."""
35        x, y, width, height = selection.get_rect()
36        self.mask[y : y + height, x : x + width] = (
37            1  # Mark selection area as fully opaque
38        )
39
40        # Update bounding rect
41        self.bounding_rect = (x, y, width, height)

Updates the mask based on selection coordinates.

def apply_mask(self, image, mask):
43    def apply_mask(self, image, mask):
44        """Applies the mask onto the image, modifying selection visibility."""
45        masked_image = image.copy()
46        masked_image[mask == 0] = (0, 0, 0)  # Hide unselected regions
47
48        return masked_image

Applies the mask onto the image, modifying selection visibility.