Skip to content

base#

This module defines an abstract base drawer.

Styles

Refer to the Styles documentation for an in-depth guide to the image processing behind Picharsso.

DEFAULT_RESAMPLING: str #

The default resampling filter.

RESAMPLING_FILTERS: dict #

A collection of resampling filters. See Pillow's Filters.

BaseDrawer #

An abstract base drawer.

Attributes:

Name Type Description
height int

The desired height of the text in characters.

width int

The desired width of the text in characters.

resample int

The resampling filter.

format Type[picharsso.format.BaseFormatter]

The formatter instance.

Note

The following methods must be overwritten:

__call__(self, image) special #

Applies processing and formatting on the image and returns a single string.

Parameters:

Name Type Description Default
image PIL.Image.Image

The subject image.

required

Returns:

Type Description
str

The string of text art.

Source code in picharsso/draw/base.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def __call__(self, image):
    """Applies processing and formatting on the `image`
    and returns a single string.

    Args:
        image (PIL.Image.Image): The subject image.

    Returns:
        str: The string of text art.
    """
    # Ensure that the image is in the `RGB` mode.
    image = ensure_rgb(image)

    # Calculate the new size of the image, for processing the text matrix.
    image_size = self.calculate_size(image.size[::-1])

    # Process text matrix from the resized image.
    text_matrix = self.process(
        image.resize(image_size[::-1], resample=self.resample)
    )

    # Apply formatting.
    return self.format(text_matrix, image, self.resample)

__init__(self, height=42, width=0, resample='nearest', **kwargs) special #

Initialization method.

Parameters:

Name Type Description Default
height Optional[int]

The desired height of the text in characters.

42
width Optional[int]

The desired width of the text in characters.

0
resample Optional[str]

The resampling filter.

'nearest'
**kwargs dict

Appropriate keyword arguments. See BaseFormatter and others.

{}

Note

When set as 0, height is derived from width and vice versa. This is done to preserve the aspect ratio of the image.

Source code in picharsso/draw/base.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def __init__(self, height=42, width=0, resample=DEFAULT_RESAMPLING, **kwargs):
    """Initialization method.

    Args:
        height (Optional[int]): The desired height of the text in characters.
        width (Optional[int]): The desired width of the text in characters.
        resample (Optional[str]): The resampling filter.
        **kwargs (dict): Appropriate keyword arguments.
                See [`BaseFormatter`][picharsso.format.base.BaseFormatter] and others.

    Note:
        When set as `0`, `height` is derived from `width` and vice versa.
        This is done to preserve the aspect ratio of the image.
    """
    self.height = None
    self.width = None
    self.resample = None
    BaseDrawer.set(self, height=height, width=width, resample=resample)

    self.format = new_formatter(**kwargs)

calculate_size(self, image_size) #

Calculates the size of the image for processing the text matrix.

Parameters:

Name Type Description Default
image_size Tuple[int, int]

The height and width of the subject image.

required

Returns:

Type Description
Tuple[int, int]

The size of the image.

Source code in picharsso/draw/base.py
 94
 95
 96
 97
 98
 99
100
101
102
103
@abstractmethod
def calculate_size(self, image_size):
    """Calculates the size of the image for processing the text matrix.

    Args:
        image_size (Tuple[int, int]): The height and width of the subject image.

    Returns:
        Tuple[int, int]: The size of the image.
    """

process(self, image) #

Converts an image to a matrix of text.

Parameters:

Name Type Description Default
image PIL.Image.Image

The subject image, with mode = "RGB", and size = (<height>, <width>).

required

Returns:

Type Description
numpy.ndarray

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

Source code in picharsso/draw/base.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@abstractmethod
def process(self, image):
    """Converts an image to a matrix of text.

    Args:
        image (PIL.Image.Image): The subject image,
                                with `mode = "RGB"`,
                                and `size = (<height>, <width>)`.

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

set(self, height=None, width=None, resample=None) #

Sets attributes of the drawer instance.

Parameters:

Name Type Description Default
height Option[int]

Sets height.

None
width Option[int]

Sets width.

None
resample Option[str]

Sets resample.

None

Exceptions:

Type Description
ValueError

If both height and width are set to 0.

Source code in picharsso/draw/base.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def set(self, height=None, width=None, resample=None):
    """Sets attributes of the drawer instance.

    Args:
        height (Option[int]): Sets `height`.
        width (Option[int]): Sets `width`.
        resample (Option[str]): Sets `resample`.

    Raises:
        ValueError: If both `height` and `width` are set to `0`.
    """
    # Set resampling filter
    if resample is not None:
        self.resample = RESAMPLING_FILTERS[resample]

    # Set height and width
    if height is not None or width is not None:
        new_h = self.height if height is None else height
        new_w = self.width if width is None else width

        if new_h == 0 and new_w == 0:
            raise ValueError("Either height or width must be non-zero")

        self.height = new_h
        self.width = new_w