Creator Module
Warning
This module is considered internal.
The Creator class is an abstract base class designed for generating images with text and applying various transformations such as noise. Subclasses must implement several abstract methods to define specific behavior for creating base images. The class supports the generation of images with customizable text, background, font, margins, and optional background images.
Attributes
The Creator class doesn’t have instance attributes but provides class-level methods for creating images. Subclasses will implement these methods to generate images based on the provided parameters.
Methods
_create_base_image()
_create_base_image(cls,
text: str,
font: ImageFont,
font_color: Tuple[int, int, int],
background_color: str,
margins: Tuple[int, int, int, int],
background_img: Image
) -> Image.Image:
This method is responsible for creating the base image with the specified text and background. It is an abstract method that must be implemented by subclasses.
-
Parameters:
-
text :
strThe text to be added to the image.
-
font :
ImageFontThe font object used to render the text.
-
font_color :
Tuple[int, int, int]The color of the text to be added.
-
background_color :
strThe color of the image background, given as a hex code or color name.
-
margins :
Tuple[int, int, int, int]The margins
(left, top, right, bottom)around the text in the image. -
background_img :
ImageAn optional background image over which the text will be placed.
-
-
Returns:
The generated
Image.
get_text_dimensions()
This method calculates the dimensions (bounding box) of the given text using the provided font.
-
Parameters:
-
text :
strThe text to measure.
-
font :
ImageFontThe font object used to render the text.
-
-
Returns:
-
A
tuplecontaining four floating-point values representing the bounding box of the text:-
left :
floatThe left boundary of the text.
-
top :
floatThe top boundary of the text.
-
right :
floatThe right boundary of the text.
-
bottom :
floatThe bottom boundary of the text.
-
get_image_dimensions()
get_image_dimensions(cls,
margins: Tuple[int, int, int, int],
text_dimensions: Tuple[float, float, float, float]
) -> Tuple[int, int]
This method calculates the width and height of the image based on the text dimensions and the provided margins.
-
Parameters:
-
margins :
Tuple[int, int, int, int]The margins (left, top, right, bottom) around the text.
-
text_dimensions :
Tuple[float, float, float, float]The bounding box of the text (left, top, right, bottom) calculated using
get_text_dimensions().
-
-
Returns:
-
A
tuplecontaining the width and height of the image as integers:-
image_width :
intThe total width of the image, including the margins.
-
image_height :
intThe total height of the image, including the margins and text area.
-
-
_apply_noise()
This method applies noise to the image, altering the appearance of the text or background based on the noise objects.
-
Parameters:
-
noises :
List[Noise]A list of Noise objects to apply to the image.
-
image :
ImageThe image object where the text will be placed and noise applied.
-
-
Returns:
The modified
Imageobject after the noise has been applied.
_blend_colors()
_blend_colors(cls, bg_color: str, text_color: str, font_opacity: float) -> Tuple[float, float, float]:
The _blend_colors method is an abstract class method intended to blend a text color with a background color based on a specified opacity level. Subclasses must implement this method to compute the blended RGB values and return them as a tuple.
-
Parameters:
-
bg_color :
strThe background color specified in any valid
PILcolor format. -
text_color :
strThe text color specified in any valid
PILcolor format. -
font_opacity :
floatThe opacity level of the text color to blend with the background color.
-
-
Returns:
tuple: A tuple representing the blended color in RGB format(R, G, B)
create_image()
create_image(cls,
text: str,
font_path: str,
noises: List[Noise],
font_size: float,
font_color: str,
font_opacity: float,
background_color: str,
margins: Tuple[int, int, int, int],
dpi: Tuple[float, float],
background_img: Image,
clear_font: bool,
) -> Image:
This is the main method responsible for creating the final image with text, background, and optional noise.
-
Parameters:
-
text :
strThe text to be rendered in the image.
-
font_path :
strThe path to the font file used to render the text.
-
noises :
List[Noise]A list of Noise objects to be applied to the image.
-
font_size :
floatThe size of the font.
-
font_color :
strThe color of the font text.
-
font_opacity :
floatThe opacity of the text, where 1.0 is fully opaque and 0.0 is fully transparent.
-
background_color :
strThe background color of the image.
-
margins :
Tuple[int, int, int, int]The margins (left, top, right, bottom) around the text.
-
dpi :
Tuple[float, float]The DPI (dots per inch) resolution for the image.
-
background_img :
ImageAn optional background image to be used instead of a plain color.
-
clear_font :
boolIf
True, the font will be rendered without noise. IfFalse, the font will also have noise applied.
-
-
Returns:
The final
Imageobject containing the text, background, and applied noise.