Apply log decorator

This commit is contained in:
Matthias Bisping 2023-02-13 17:22:01 +01:00
parent 2bcac91dea
commit e952d19c68

View File

@ -3,15 +3,17 @@ from typing import List
from PIL import Image, ImageDraw, ImageFont
from funcy import first
from cv_analysis.logging import dev_logger, dev_log
from cv_analysis.utils.image_operations import superimpose
from cv_analysis.utils.rectangle import Rectangle
from synthesis.segment.content_rectangle import ContentRectangle
from synthesis.text.text_block_generator.paragraph import ParagraphGenerator
from synthesis.text.font import pick_random_mono_space_font_available_on_system
from synthesis.text.text_block_generator.paragraph import ParagraphGenerator
class TextBlock(ContentRectangle):
def __init__(self, x1, y1, x2, y2, text_generator=None, font=None, font_size=None):
dev_logger.trace(f"Creating text block at {x1, y1, x2, y2}.")
super().__init__(x1, y1, x2, y2)
self.font = font or ImageFont.load_default() # pick_random_font_available_on_system(size=font_size)
self.text_generator = text_generator or ParagraphGenerator()
@ -19,11 +21,13 @@ class TextBlock(ContentRectangle):
def __call__(self, *args, **kwargs):
pass
def generate_random_text(self, rectangle: Rectangle, n_sentences=3000):
@dev_log()
def generate_random_text(self, rectangle: Rectangle, n_sentences=30):
lines = self.text_generator(rectangle, n_sentences)
image = write_lines_to_image(lines, rectangle, self.font)
return self.__put_content(image)
@dev_log()
def put_text(self, text: str, rectangle: Rectangle):
text_width, text_height = self.font.getsize(text)
@ -40,13 +44,16 @@ class TextBlock(ContentRectangle):
draw.text((0, 0), text, font=self.font, fill=(0, 0, 0, 255))
return self.__put_content(image)
@dev_log()
def __put_content(self, image: Image.Image):
self.content = image if not self.content else superimpose(self.content, image)
assert self.content.mode == "RGBA"
return self
@dev_log()
def write_lines_to_image(lines: List[str], rectangle: Rectangle, font=None) -> Image.Image:
@dev_log()
def write_line(line, line_number):
draw.text((0, line_number * text_size), line, font=font, fill=(0, 0, 0, 255))