API Reference¶
Complete API documentation for TextBaker.
TextGenerator¶
Main class for programmatic text generation.
TextGenerator
¶
Programmatic text image generator.
This class provides an API for generating text images without using the GUI. It's designed for library usage, batch processing, and integration.
Example
from textbaker import TextGenerator, GeneratorConfig
# Simple usage with defaults
generator = TextGenerator()
result = generator.generate("hello")
cv2.imwrite("hello.png", result.image)
# With custom config
config = GeneratorConfig(
seed=42,
spacing=5,
)
generator = TextGenerator(config)
# Generate multiple samples
for i in range(10):
result = generator.generate_random()
generator.save(result, f"sample_{i}.png")
available_characters
property
¶
Get list of available character labels.
__init__(config=None)
¶
Initialize the text generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Optional[GeneratorConfig]
|
Generator configuration. Uses defaults if not provided. |
None
|
generate(text)
¶
Generate an image for the given text.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to generate image for |
required |
Returns:
| Type | Description |
|---|---|
GenerationResult
|
GenerationResult with image, text, and labels |
generate_random(length=None)
¶
Generate a random text image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
Optional[int]
|
Text length. If None, randomly chosen from config range. |
None
|
Returns:
| Type | Description |
|---|---|
GenerationResult
|
GenerationResult with generated image |
save(result, filename=None, output_dir=None)
¶
Save generation result to file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
GenerationResult
|
GenerationResult to save |
required |
filename
|
Optional[str]
|
Output filename. Auto-generated if not provided. |
None
|
output_dir
|
Optional[Path]
|
Output directory. Uses config if not provided. |
None
|
Returns:
| Type | Description |
|---|---|
Path
|
Path to saved file |
batch_generate(texts, save=True, output_dir=None)
¶
Generate images for multiple texts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texts
|
list[str]
|
List of texts to generate |
required |
save
|
bool
|
Whether to save results to disk |
True
|
output_dir
|
Optional[Path]
|
Output directory for saved files |
None
|
Returns:
| Type | Description |
|---|---|
list[GenerationResult]
|
List of GenerationResult objects |
reset_seed(seed=None)
¶
Reset the random seed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seed
|
Optional[int]
|
New seed. Uses config seed if not provided. |
None
|
Configuration Classes¶
GeneratorConfig¶
Main configuration class for text generation.
GeneratorConfig
¶
Bases: BaseConfig
Main configuration for text generation.
This is the primary config class for programmatic usage.
Example
from_file(path)
classmethod
¶
Load configuration from a JSON or YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Path to config file |
required |
Returns:
| Type | Description |
|---|---|
GeneratorConfig
|
GeneratorConfig instance |
to_file(path)
¶
Save configuration to a JSON or YAML file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Union[str, Path]
|
Path to save config file |
required |
TransformConfig¶
Configuration for image transformations.
TransformConfig
¶
Bases: BaseConfig
Configuration for image transformations.
ColorConfig¶
Configuration for color manipulation.
ColorConfig
¶
Bases: BaseConfig
Configuration for color manipulation.
TextureConfig¶
Configuration for texture application.
TextureConfig
¶
Bases: BaseConfig
Configuration for texture application.
BackgroundConfig¶
Configuration for background handling.
BackgroundConfig
¶
Bases: BaseConfig
Configuration for background handling.
DatasetConfig¶
Configuration for dataset loading.
DatasetConfig
¶
Bases: BaseConfig
Configuration for dataset paths.
OutputConfig¶
Configuration for output settings.
OutputConfig
¶
Bases: BaseConfig
Configuration for output settings.
Data Classes¶
GenerationResult¶
Result of a text generation operation.
GenerationResult
dataclass
¶
Result of text generation.
GeneratedText¶
Generated text with metadata.
GeneratedText
dataclass
¶
Represents generated text with its images and labels.
Random State¶
Global random state for reproducibility.
from textbaker import rng
# Set seed for reproducibility
rng.seed(42)
# Get current seed
print(rng.current_seed)
# Random operations
rng.randint(0, 100) # Random integer in range
rng.uniform(0.0, 1.0) # Random float in range
rng.choice(items) # Random choice from list
rng.choices(items, k=5) # Multiple random choices
rng.shuffle(items) # Shuffle list in place
CLI Commands¶
textbaker gui¶
Launch the graphical user interface.
Options:
| Option | Short | Default | Description |
|---|---|---|---|
--dataset |
-d |
assets/dataset |
Character dataset folder |
--output |
-o |
output |
Output folder |
--backgrounds |
-b |
assets/backgrounds |
Background images folder |
--textures |
-t |
assets/textures |
Texture images folder |
--seed |
-s |
42 |
Random seed |
--config |
-c |
Config file path |
textbaker generate¶
Generate images from the command line.
Arguments:
TEXTS: Text strings to generate (optional, uses random if not provided)
Options:
| Option | Short | Description |
|---|---|---|
--config |
-c |
Config file path |
--dataset |
-d |
Dataset folder |
--output |
-o |
Output folder |
--seed |
-s |
Random seed |
--count |
-n |
Number of random samples |
--length |
-l |
Text length for random generation |
--spacing |
Spacing between characters | |
--rotation |
-r |
Rotation range (e.g., "-15,15") |
--perspective |
-p |
Perspective range (e.g., "0,0.1") |
--scale |
Scale range (e.g., "0.8,1.2") | |
--shear |
Shear range (e.g., "-10,10") | |
--backgrounds |
-b |
Background images folder |
--bg-color |
Solid color (e.g., "255,255,255") | |
--textures |
-t |
Texture images folder |
--texture-opacity |
Texture opacity (0.0-1.0) | |
--random-color |
Apply random colors | |
--color |
Fixed color (e.g., "0,0,255") |
textbaker init-config¶
Create a default configuration file.
Options:
| Option | Short | Default | Description |
|---|---|---|---|
--output |
-o |
textbaker_config.yaml |
Output file path |
--format |
-f |
yaml |
Format (json or yaml) |