Skip to content

pycht.py

Main project logic for generating color-separated stencils from an input image.

Pycht

Main interface for generating color-separated stencils from an input image.

This class orchestrates the image processing and clustering steps by using the ImageProcessing and Clustering components.

Source code in pycht/pycht.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Pycht:
    """
    Main interface for generating color-separated stencils from an input image.

    This class orchestrates the image processing and clustering steps by
    using the `ImageProcessing` and `Clustering` components.
    """

    def __init__(self, image_processor: ImageProcessing = None, clustering_model: Clustering = None) -> None:
        self.image_processing = image_processor or ImageProcessing()
        self.clustering = clustering_model or Clustering()

    def stencil(self, input_img: str | Path, nb_colors: int = 3, output_path: str | Path = "./") -> None:
        """
        Generate color stencils from an input image using K-Means clustering.

        Parameters
        ----------
        input_img : Path or str
            Path to the input image file.
        output_path : Path or str
            Directory path to save the stencil images.
        nb_colors : int
            Number of color clusters to segment the image into.
        """
        input_img = Path(input_img)
        output_path = Path(output_path)

        flattened_img = self.image_processing.process(input_img)
        clustered_img = self.clustering.compute(flattened_img, nb_colors)
        self.image_processing.color_separation(clustered_img, input_img, output_path)

stencil(input_img, nb_colors=3, output_path='./')

Generate color stencils from an input image using K-Means clustering.

Parameters

input_img : Path or str Path to the input image file. output_path : Path or str Directory path to save the stencil images. nb_colors : int Number of color clusters to segment the image into.

Source code in pycht/pycht.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def stencil(self, input_img: str | Path, nb_colors: int = 3, output_path: str | Path = "./") -> None:
    """
    Generate color stencils from an input image using K-Means clustering.

    Parameters
    ----------
    input_img : Path or str
        Path to the input image file.
    output_path : Path or str
        Directory path to save the stencil images.
    nb_colors : int
        Number of color clusters to segment the image into.
    """
    input_img = Path(input_img)
    output_path = Path(output_path)

    flattened_img = self.image_processing.process(input_img)
    clustered_img = self.clustering.compute(flattened_img, nb_colors)
    self.image_processing.color_separation(clustered_img, input_img, output_path)