def test_seed(): text = get_short_text() * 50 template2 = get_default_template2() for seed in (0, "PyLf"): ims1 = handwrite2(text, template2, seed=seed) ims2 = handwrite2(text, template2, seed=seed) for im1, im2 in zip(ims1, ims2): assert im1 == im2
def test_result(): assert isinstance(handwrite2("", get_default_template2()), list) assert isinstance( handwrite2(get_short_text(), get_default_template2()), list ) assert isinstance( handwrite2(get_long_text(), get_default_template2()), list )
def test_seed(): text = get_long_text() template2 = get_default_template2() for seed in (0, "PyLf"): images1 = handwrite2(text, template2, seed=seed) images2 = handwrite2(text, template2, seed=seed) assert all(im1 == im2 for im1, im2 in zip(images1, images2)) images1 = handwrite2(text, template2, seed=None) images2 = handwrite2(text, template2, seed=None) assert not all(im1 == im2 for im1, im2 in zip(images1, images2))
def test_seed(): text = get_long_text() template2 = get_default_template2() for seed in (0, "PyLf"): images1 = handwrite2(text, template2, seed=seed) images2 = handwrite2(text, template2, seed=seed) assert images1 == images2 images1 = handwrite2(text, template2, seed=None) images2 = handwrite2(text, template2, seed=None) assert not images1 == images2
def test_1_image(): text = get_long_text() template2 = get_default_template2() template2["backgrounds"] = [ PIL.Image.new("L", SIZE, color="white"), PIL.Image.new("L", SIZE, color="white"), ] criterion = handwrite2(text, template2, seed=SEED) template2["backgrounds"] = [ PIL.Image.new("1", SIZE, color="white"), PIL.Image.new("1", SIZE, color="white"), ] images = handwrite2(text, template2, seed=SEED) criterion = [image.convert("1") for image in criterion] assert all(im1 == im2 for im1, im2 in zip(criterion, images))
def test_one_background(): background = PIL.Image.new(mode="RGB", size=DEFAULT_SIZE, color="white") margin = {"left": 200, "top": 376, "right": 200, "bottom": 400} line_spacing = 144 font = get_default_font() font_size = 120 font_size_sigma = 0 text = get_long_text() template = { "background": background, "margin": margin, "line_spacing": line_spacing, "font": font, "font_size": font_size, "font_size_sigma": font_size_sigma, } template2 = { "backgrounds": (background, ), "margins": (margin, ), "line_spacings": (line_spacing, ), "font_sizes": (font_size, ), "font_size_sigmas": (font_size_sigma, ), "font": font, } images1 = handwrite(text, template, seed=SEED) images2 = handwrite2(text, template2, seed=SEED) for im1, im2 in zip(images1, images2): assert im1 == im2
def test_rgba_image(): text = get_long_text() template2 = get_default_template2() template2["color"] = "red" template2["backgrounds"] = [ PIL.Image.new("RGB", SIZE, color="white"), PIL.Image.new("RGB", SIZE, color="pink"), ] criterion = handwrite2(text, template2, seed=SEED) template2["backgrounds"] = [ PIL.Image.new("RGBA", SIZE, color="white"), PIL.Image.new("RGBA", SIZE, color="pink"), ] images = handwrite2(text, template2, seed=SEED) images = [image.convert("RGB") for image in images] assert all(im1 == im2 for im1, im2 in zip(criterion, images))
def test_l_image(): text = get_long_text() template2 = get_default_template2() template2["color"] = "orange" template2["backgrounds"] = [ PIL.Image.new("RGB", SIZE, color="yellow"), PIL.Image.new("RGB", SIZE, color="black"), ] criterion = handwrite2(text, template2, seed=SEED) template2["backgrounds"] = [ PIL.Image.new("L", SIZE, color="yellow"), PIL.Image.new("L", SIZE, color="black"), ] images = handwrite2(text, template2, seed=SEED) criterion = [image.convert("L") for image in criterion] assert criterion == images
def test_one_background(): background = PIL.Image.new(mode="RGB", size=SIZE, color="white") margin = {"left": 10, "top": 18, "right": 10, "bottom": 20} line_spacing = 7 font = get_default_font() font_size = 6 text = get_long_text() template = { "background": background, "margin": margin, "line_spacing": line_spacing, "font": font, "font_size": font_size, } template2 = { "backgrounds": (background,), "margins": (margin,), "line_spacings": (line_spacing,), "font_sizes": (font_size,), "font": font, } images1 = handwrite(text, template, seed=SEED) images2 = handwrite2(text, template2, seed=SEED) assert images1 == images2
def main(): template2 = dict( page_settings=[ dict( background=Image.open("./pictures/even-odd-letter/村庄信笺纸.jpg"), box=(20, 107, 1285, 1110), font_size=37, ), dict( background=Image.open("./pictures/even-odd-letter/树信笺纸.jpg"), box=(20, 107, 1285, 900), font_size=37, ), ], font=ImageFont.truetype( "./fonts/Bo Le Locust Tree Handwriting Pen Chinese Font-Simplified Chinese Fonts.ttf" ), ) images = handwrite2(TEXT, template2) # 为了方便展示,将生成图片合并成一张图片。 assert images width, height = images[0].size res = Image.new(images[0].mode, size=(width * len(images), height)) for i, image in enumerate(images): res.paste(image, box=(i * width, 0)) res.save("./out/从百草园到三味书屋.png")
def test_even_odd(self): text = self.get_default_text() template2 = self.get_default_template2() ims = handwrite2(text, template2, anti_aliasing=False) for i, im in enumerate(ims): # im.save("./data/images/test_even_odd{}.png".format(i)) self.assertTrue( self.compare( im, PIL.Image.open( "./data/images/test_even_odd{}.png".format(i))))
def test_even_odd(): text = get_short_text() template2 = get_default_template2() standard_images = [] for i in range(2): font_size = template2["font_sizes"][i] standard_image = template2["backgrounds"][i].copy() xy = ( template2["margins"][i]["left"], template2["margins"][i]["top"] + template2["line_spacings"][i] - template2["font_sizes"][i], ) draw = PIL.ImageDraw.Draw(standard_image) draw.text( xy=xy, text=text, fill=template2["color"], font=template2["font"].font_variant(size=font_size), ) standard_images.append(standard_image) images2 = handwrite2((text + "\n" * 8) * 6, template2) for i in range(len(images2)): assert diff_histogram(images2[i], standard_images[i % 2]) < THRESHOLD