Download tests/test_drip.py from Kazzze/sd-comic-lettering: direct link, hf CLI and curl.
- Browser
- Download file 4.91 kB
-
https://huggingface.co/Kazzze/sd-comic-lettering/resolve/main/tests/test_drip.py
- Command line
-
hf download hf://Kazzze/sd-comic-lettering/tests/test_drip.py
-
curl -L -o test_drip.py https://huggingface.co/Kazzze/sd-comic-lettering/resolve/main/tests/test_drip.py
4.91 kB
| """Dripping letters, a dripping bubble and a glow round the caption's bubble. | |
| Run from the repository folder: python -m pytest tests | |
| """ | |
| import os | |
| import sys | |
| from typing import Tuple | |
| import numpy as np | |
| from PIL import Image | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| from comic_lettering import BUBBLE_SHAPES, Style, letter, web # noqa: E402 | |
| SIZE = (400, 240) | |
| DARK = (20, 22, 30) | |
| GREEN = "#3dff7a" | |
| MIN_PIXELS = 40 | |
| LOOK = dict(caption_colors="custom", text_color=GREEN, stroke_width=0.0, shadow=0.0, soften=0.0, jitter=0.0, font="Bangers", | |
| caption_font="Bangers", free_size=22) | |
| def shown(**more): | |
| # type: (**object) -> np.ndarray | |
| base = Image.new("RGB", SIZE, DARK) | |
| style = Style(**dict(LOOK, **more)) | |
| return np.asarray(letter(base, caption="OOHHHH", placement="center", style=style)[0]).astype(int) | |
| def inked(picture): | |
| # type: (np.ndarray) -> np.ndarray | |
| return (np.abs(picture - np.array(DARK)).sum(axis=2) > 60) | |
| def rows(mask): | |
| # type: (np.ndarray) -> Tuple[int, int] | |
| ys = np.nonzero(mask.any(axis=1))[0] | |
| return int(ys.min()), int(ys.max()) | |
| def test_no_drip_is_the_picture_of_before(): | |
| # type: () -> None | |
| assert np.array_equal(shown(), shown(drip=0.0)) | |
| def test_drips_run_down_from_the_letters_and_only_down(): | |
| # type: () -> None | |
| plain, dripping = inked(shown()), inked(shown(drip=0.8)) | |
| assert dripping.sum() > plain.sum() + MIN_PIXELS | |
| assert rows(dripping)[0] == rows(plain)[0], "nothing grows above the letters" | |
| assert rows(dripping)[1] > rows(plain)[1], "something hangs below them" | |
| assert (plain & ~dripping).sum() == 0, "the letters themselves are untouched" | |
| def test_more_drip_is_more_and_longer(): | |
| # type: () -> None | |
| low, high = inked(shown(drip=0.2)), inked(shown(drip=1.0)) | |
| assert high.sum() > low.sum() | |
| assert rows(high)[1] >= rows(low)[1] | |
| def test_drips_are_the_same_every_time_and_change_with_the_seed(): | |
| # type: () -> None | |
| assert np.array_equal(shown(drip=0.7), shown(drip=0.7)) | |
| assert not np.array_equal(shown(drip=0.7, seed=1), shown(drip=0.7, seed=2)) | |
| def test_drips_reach_the_outline_too(): | |
| # type: () -> None | |
| with_outline = dict(stroke_color="#ff0000", stroke_width=0.2) | |
| plain, dripping = shown(**with_outline), shown(drip=0.8, **with_outline) | |
| red = lambda picture: ((picture[..., 0] > 150) & (picture[..., 1] < 80)).sum() # noqa: E731 | |
| assert red(dripping) > red(plain), "the drips have an outline round them" | |
| def test_a_dripping_bubble_is_a_choice_and_hangs_below_an_oval(): | |
| # type: () -> None | |
| assert "drip" in BUBBLE_SHAPES | |
| assert web.merge_values({"caption_bubble": "drip"})["caption_bubble"] == "drip" | |
| base = Image.new("RGB", SIZE, (112, 118, 126)) | |
| pictures = {} | |
| for shape in ("oval", "drip"): | |
| pictures[shape] = np.asarray(letter(base, caption="Mmhh", placement="center", style=Style(caption_bubble=shape, caption_bubble_theme="dark"))[0]).astype(int) | |
| dark = lambda picture: (picture.sum(axis=2) < 150) # noqa: E731 | |
| assert dark(pictures["drip"]).sum() > dark(pictures["oval"]).sum() | |
| assert rows(dark(pictures["drip"]))[1] > rows(dark(pictures["oval"]))[1] | |
| # The bubble is placed by what is drawn, drips included, so its body stands a little higher than an oval's. | |
| assert abs(rows(dark(pictures["drip"]))[0] - rows(dark(pictures["oval"]))[0]) < 12 | |
| def test_a_glow_round_the_bubble_keeps_the_bubble_where_it_is(): | |
| # type: () -> None | |
| base = Image.new("RGB", SIZE, (112, 118, 126)) | |
| look = dict(caption_bubble="oval", caption_bubble_theme="dark", caption_colors="custom", text_color=GREEN) | |
| plain = np.asarray(letter(base, caption="Mitsuki", placement="center", style=Style(**look))[0]).astype(int) | |
| glowing = np.asarray(letter(base, caption="Mitsuki", placement="center", style=Style(caption_bubble_glow=0.8, **look))[0]).astype(int) | |
| assert np.array_equal(plain, np.asarray(letter(base, caption="Mitsuki", placement="center", style=Style(caption_bubble_glow=0.0, **look))[0]).astype(int)) | |
| greener = glowing[..., 1] - glowing[..., 0] | |
| assert (greener > 40).sum() > (plain[..., 1] - plain[..., 0] > 40).sum() + MIN_PIXELS, "a green glow appears" | |
| body = lambda picture: picture.sum(axis=2) < 150 # noqa: E731 | |
| middle = lambda picture: tuple(np.nonzero(body(picture))[axis].mean() for axis in (0, 1)) # noqa: E731 | |
| assert all(abs(a - b) < 2 for a, b in zip(middle(plain), middle(glowing))), "the bubble does not move" | |
| def test_the_new_settings_are_remembered_and_kept_in_range(): | |
| # type: () -> None | |
| values = web.merge_values({"drip": 5, "caption_bubble_glow": -2}) | |
| assert values["drip"] == 1.0 and values["caption_bubble_glow"] == 0.0 | |
| assert web.merge_values({})["drip"] == 0.0 | |