def test_image(): mode = "RGB" size = (1, 1) color = "white" im = PIL.Image.new(mode, size, color) page = _page.Page(mode, size, color, 0) assert page.image == im
def _draw_text(text: str, page_settings: list, font, is_half_char, is_end_char, anti_aliasing: bool, seed: int) -> list: """Draws the text randomly in black images with white color. Note that (box[3] - box[1]) and (box[2] - box[0]) both must be greater than corresponding font_size. """ # Avoid dead loops for page_setting in page_settings: if not page_setting['box'][3] - page_setting['box'][1] > page_setting[ 'font_size']: raise ValueError( "(box[3] - box[1]) must be greater than corresponding font_size." ) if not page_setting['box'][2] - page_setting['box'][0] > page_setting[ 'font_size']: raise ValueError( "(box[2] - box[0]) must be greater than corresponding font_size." ) rand = random.Random(x=seed) length = len(page_settings) chars = iter(text) pages = [] try: char = next(chars) index = 0 while True: (size, box, font_size, word_spacing, line_spacing, font_size_sigma, line_spacing_sigma, word_spacing_sigma) = _parse_page_setting( page_settings[index % length], anti_aliasing) left, upper, right, lower = box page = _page.Page(_INTERNAL_MODE, size, _BLACK, index) draw = page.draw y = upper try: while y < lower - font_size: x = left while True: if char == '\n': char = next(chars) break if x >= right - font_size and not is_end_char(char): break actual_font_size = max( int(rand.gauss(font_size, font_size_sigma)), 0) xy = (x, int(rand.gauss(y, line_spacing_sigma))) font = font.font_variant(size=actual_font_size) offset = _draw_char(draw, char, xy, font) x_step = word_spacing + offset * ( 0.5 if is_half_char(char) else 1) x += int(rand.gauss(x_step, word_spacing_sigma)) char = next(chars) y += line_spacing + font_size pages.append(page) except StopIteration: pages.append(page) raise StopIteration() index += 1 except StopIteration: return pages
def _draw_pages( text: str, sizes: Sequence[Tuple[int, int]], top_margins: Sequence[int], bottom_margins: Sequence[int], left_margins: Sequence[int], right_margins: Sequence[int], line_spacings: Sequence[int], font_sizes: Sequence[int], word_spacings: Sequence[int], line_spacing_sigmas: Sequence[float], font_size_sigmas: Sequence[float], word_spacing_sigmas: Sequence[float], font, is_half_char_fn: Callable[[str], bool], is_end_char_fn: Callable[[str], bool], seed: Optional[Hashable], ) -> Iterator[_page.Page]: sizes = itertools.cycle(sizes) top_margins = itertools.cycle(top_margins) bottom_margins = itertools.cycle(bottom_margins) left_margins = itertools.cycle(left_margins) right_margins = itertools.cycle(right_margins) line_spacings = itertools.cycle(line_spacings) font_sizes = itertools.cycle(font_sizes) word_spacings = itertools.cycle(word_spacings) line_spacing_sigmas = itertools.cycle(line_spacing_sigmas) font_size_sigmas = itertools.cycle(font_size_sigmas) word_spacing_sigmas = itertools.cycle(word_spacing_sigmas) nums = itertools.count() rand = random.Random(x=seed) start = 0 while start < len(text): page = _page.Page(_INTERNAL_MODE, next(sizes), _BLACK, next(nums)) start = _draw_page( page, text, start, top_margin=next(top_margins), bottom_margin=next(bottom_margins), left_margin=next(left_margins), right_margin=next(right_margins), line_spacing=next(line_spacings), font_size=next(font_sizes), word_spacing=next(word_spacings), line_spacing_sigma=next(line_spacing_sigmas), font_size_sigma=next(font_size_sigmas), word_spacing_sigma=next(word_spacing_sigmas), font=font, is_half_char_fn=is_half_char_fn, is_end_char_fn=is_end_char_fn, rand=rand, ) yield page
def test_height(): size = (1, 2) page = _page.Page("CMYK", size, "white", 0) assert page.height() == size[1]
def test_width(): size = (1, 2) page = _page.Page("CMYK", size, "white", 0) assert page.width() == size[0]
def test_size(): size = (1, 2) page = _page.Page("CMYK", size, "white", 0) assert page.size() == size
def test_num(): num = 3 page = _page.Page("L", (1, 1), "white", num) assert page.num == num