Skip to content

gradient#

This module defines a drawer for the gradient style.

Example

Consider the following image:

Apple logo

Apple Computer [Rob Janoff, 1977]

Here's what it should look like:

Apple logo in text (gradient style)

DEFAULT_CHARSET: str #

The default character set.

GradientDrawer #

A drawer for the gradient style.

Inherits BaseDrawer.

Attributes:

Name Type Description
charset str

A set of characters ordered by the amount of area their symbols occupy.

negative bool

Whether or not to reverse the charset.

charset_array numpy.ndarray

A vectorized version of the charset.

__init__(self, charset=' :!?PG@', negative=False, **kwargs) special #

Initialization method.

Parameters:

Name Type Description Default
charset Optional[str]

A set of characters ordered by the amount of area their symbols occupy. Defaults to DEFAULT_CHARSET

' :!?PG@'
negative Optional[bool]

Whether or not to reverse the charset.

False
**kwargs dict

Appropriate keyword arguments. See BaseDrawer.

{}
Source code in picharsso/draw/gradient.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def __init__(self, charset=DEFAULT_CHARSET, negative=False, **kwargs):
    """Initialization method.

    Args:
        charset (Optional[str]): A set of characters ordered
                                by the amount of area their symbols occupy.
                                Defaults to `DEFAULT_CHARSET`
        negative (Optional[bool]): Whether or not to reverse the `charset`.
        **kwargs (dict): Appropriate keyword arguments.
                        See [`BaseDrawer`][picharsso.draw.base.BaseDrawer].
    """
    super().__init__(**kwargs)
    self.charset = None
    self.negative = None
    self.charset_array = None
    self.set(charset=charset, negative=negative)

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/gradient.py
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
def calculate_size(self, image_size):
    # Possible dimensions
    new_h = self.height
    new_w = self.width

    # Image dimensions
    old_h, old_w = image_size

    # If height is not set, infer it from width
    if not new_h:
        new_h = int(round(old_h / old_w * new_w / 2.125))

    # If width is not set, infer it from height
    if not new_w:
        new_w = int(round(old_w / old_h * new_h * 2.125))

    return new_h, new_w

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/gradient.py
82
83
84
85
86
87
88
89
90
91
def process(self, image):
    # Convert the image mode to grayscale.
    # Normalize the pixel values from a range of (0, 255) to (0, len(self.charset)-1),
    # to obtain indices for the character set.
    # Index the character set array with the indices.
    return self.charset_array[
        np.round(
            np.array(image.convert("L")) / 255 * (len(self.charset) - 1)
        ).astype(int)
    ]

set(self, charset=None, negative=None, **kwargs) #

Sets attributes of the drawer instance.

Parameters:

Name Type Description Default
charset Optional[str]

Sets charset.

None
negative Optional[bool]

Sets negative.

None
**kwargs dict

Appropriate keyword arguments. See BaseDrawer.set.

{}
Source code in picharsso/draw/gradient.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
def set(self, charset=None, negative=None, **kwargs):
    """Sets attributes of the drawer instance.

    Args:
        charset (Optional[str]): Sets `charset`.
        negative (Optional[bool]): Sets `negative`.
        **kwargs (dict): Appropriate keyword arguments.
                See [`BaseDrawer.set`][picharsso.draw.base.BaseDrawer.set].
    """
    super().set(**kwargs)

    if charset is not None:
        self.charset = charset

    if negative is not None:
        self.negative = negative

    self.charset_array = np.array(
        list(self.charset if not self.negative else self.charset[::-1])
    )