Skip to content

base#

This module defines an abstract base formatter.

Formats

Refer to the Formats documentation to learn about the supported output formats.

BaseFormatter #

An abstract base formatter.

Attributes:

Name Type Description
colorize bool

Whether to color the text.

vcolor Callable

The vectorized implementation of the color method.

Note

The following methods must be overwritten:

__call__(self, text_matrix, image, resample) special #

Applies formatting and colorization on the text_matrix and returns a single string.

Parameters:

Name Type Description Default
text_matrix numpy.ndarray

The subject text matrix, with shape = (<height>, <width>), and dtype = str.

required
image PIL.Image.Image

The subject image.

required
resample int

The resampling filter.

required

Returns:

Type Description
str

The formatted string of text with color (if specified).

Source code in picharsso/format/base.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def __call__(self, text_matrix, image, resample):
    """Applies formatting and colorization on the `text_matrix`
    and returns a single string.

    Args:
        text_matrix (numpy.ndarray): The subject text matrix,
                                    with `shape = (<height>, <width>)`,
                                    and `dtype = str`.
        image (PIL.Image.Image): The subject image.
        resample (int): The resampling filter.

    Returns:
        str: The formatted string of text with color (if specified).
    """

    text_size = text_matrix.shape

    # Apply any translations.
    text_matrix = self.translate(text_matrix)

    # Colorize if necessary
    if self.colorize:
        # Pool the colors from the original image by resizing it to the size of the text output.
        # Using the vectorized `color` method, color each element in the `text_martix`.
        # The vectorized operation takes a `str` from `text_matrix`
        # and a `List[int, int, int]` from the pooled colors.
        text_matrix = self.vcolor(
            text_matrix,
            unstructured_to_structured(
                np.array(image.resize(text_size[::-1], resample=resample)).astype(
                    np.uint8
                )
            ).astype("O"),
        )

    return self.unify(text_matrix)

__init__(self, colorize=False) special #

Initialization method.

Parameters:

Name Type Description Default
colorize Option[bool]

Whether to color the text.

False
Source code in picharsso/format/base.py
31
32
33
34
35
36
37
38
39
40
def __init__(self, colorize=False):
    """Initialization method.

    Args:
        colorize (Option[bool]): Whether to color the text.
    """
    self.colorize = None
    BaseFormatter.set(self, colorize=colorize)

    self.vcolor = np.vectorize(self.color)

color(text, color) staticmethod #

Applies color to a string of text.

Parameters:

Name Type Description Default
text str

The subject text.

required
color Tuple[int, int, int]

The RGB value for the color.

required

Returns:

Type Description
str

The colored text.

Source code in picharsso/format/base.py
79
80
81
82
83
84
85
86
87
88
89
90
@staticmethod
@abstractmethod
def color(text, color):
    """Applies `color` to a string of `text`.

    Args:
        text (str): The subject text.
        color (Tuple[int, int, int]): The `RGB` value for the color.

    Returns:
        str: The colored text.
    """

set(self, colorize=None) #

Sets attributes of the formatter instance.

Parameters:

Name Type Description Default
colorize Optional[bool]

Sets colorize.

None
Source code in picharsso/format/base.py
120
121
122
123
124
125
126
127
def set(self, colorize=None):
    """Sets attributes of the formatter instance.

    Args:
        colorize (Optional[bool]): Sets `colorize`.
    """
    if colorize is not None:
        self.colorize = colorize

translate(text_matrix) staticmethod #

Applies translatations to text_matrix.

Parameters:

Name Type Description Default
text_matrix numpy.ndarray

The subject text matrix, with shape = (<height>, <width>), and dtype = str.

required

Returns:

Type Description
numpy.ndarray

The translated text_matrix.

Source code in picharsso/format/base.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
@staticmethod
@abstractmethod
def translate(text_matrix):
    """Applies translatations to `text_matrix`.

    Args:
        text_matrix (numpy.ndarray): The subject text matrix,
                                    with `shape = (<height>, <width>)`,
                                    and `dtype = str`.

    Returns:
        numpy.ndarray: The translated text_matrix.
    """

unify(text_matrix) staticmethod #

Formats a text_matrix into a single string.

Parameters:

Name Type Description Default
text_matrix numpy.ndarray

The subject text matrix, with shape = (<height>, <width>), and dtype = str.

required

Returns:

Type Description
str

The formatted string of text art.

Source code in picharsso/format/base.py
106
107
108
109
110
111
112
113
114
115
116
117
118
@staticmethod
@abstractmethod
def unify(text_matrix):
    """Formats a `text_matrix` into a single string.

    Args:
        text_matrix (numpy.ndarray): The subject text matrix,
                                    with `shape = (<height>, <width>)`,
                                    and `dtype = str`.

    Returns:
        str: The formatted string of text art.
    """