Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bipinkrish committed Apr 10, 2024
1 parent b64d0ba commit 29c0475
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 29 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 38 additions & 25 deletions signwriting/visualizer/test_visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,27 @@


class VisualizeCase(unittest.TestCase):
def test_image_fsw(self):
fsw = "AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S20544510x500S10019476x475"
image = signwriting_to_image(fsw)

def assert_image_equal_with_reference(self, fsw, image):
assets_path = Path(__file__).parent / "test_assets" / f"{fsw}.png"
reference_image = Image.open(assets_path)

self.assertEqual(reference_image.size, image.size)
assert_array_equal(np.array(image), np.array(reference_image))

def test_image_fsw(self):
fsw = "AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S20544510x500S10019476x475"
image = signwriting_to_image(fsw)
self.assert_image_equal_with_reference(fsw, image)

def test_image_without_antialiasing(self):
fsw = "M528x526S1ce40506x474S1ce48472x474S22a04507x511S22a14480x510"
image = signwriting_to_image(fsw, antialiasing=False)

assets_path = Path(__file__).parent / "test_assets" / f"{fsw}.png"
reference_image = Image.open(assets_path)

self.assertEqual(reference_image.size, image.size)
assert_array_equal(np.array(image), np.array(reference_image))
self.assert_image_equal_with_reference(fsw, image)

def test_image_invalid_fsw(self):
fsw = "S20555"
image = signwriting_to_image(fsw)

assets_path = Path(__file__).parent / "test_assets" / f"{fsw}.png"
reference_image = Image.open(assets_path)

assert_array_equal(np.array(image), np.array(reference_image))
self.assert_image_equal_with_reference(fsw, image)

def test_image_small_box(self):
fsw = "M500x500S2ff00407x501S1ce20436x535S2e300413x552S22b04418x565S36520420x523"
Expand All @@ -47,21 +40,41 @@ def test_image_small_box(self):
def test_image_small_box_is_corrected(self):
fsw = "M500x500S2ff00407x501S1ce20436x535S2e300413x552S22b04418x565S36520420x523"
image = signwriting_to_image(fsw, trust_box=False)
self.assert_image_equal_with_reference(fsw, image)

assets_path = Path(__file__).parent / "test_assets" / f"{fsw}.png"
reference_image = Image.open(assets_path)
def test_image_with_line_color(self):
fsw = "M518x517S10040482x485S26500493x474"
image = signwriting_to_image(fsw, line_color=(123, 234, 0, 255))
self.assert_image_equal_with_reference(fsw, image)

assert_array_equal(np.array(image), np.array(reference_image))
def test_image_with_fill_color(self):
fsw = "M536x517S30a00482x482S33e00482x482S17e11516x464S17e19460x481S22a07523x481S22a11463x481"
image = signwriting_to_image(fsw, fill_color=(123, 234, 0, 255))
self.assert_image_equal_with_reference(fsw, image)

def test_image_with_line_and_embedded_color(self):
fsw = "M530x518S19a30500x482S19a38465x481S22f04509x506S22f14467x504"
image = signwriting_to_image(fsw, line_color=(
10, 23, 122, 255), fill_color=(255, 255, 255, 0))
def test_image_with_embedded_color(self):
fsw = "M518x517S10043487x482S20500487x507"
image = signwriting_to_image(fsw, embedded_color=True)
self.assert_image_equal_with_reference(fsw, image)

assets_path = Path(__file__).parent / "test_assets" / f"{fsw}.png"
reference_image = Image.open(assets_path)
def test_image_with_line_and_fill_color(self):
fsw = "M530x518S19a30500x482S19a38465x481S22f04509x506S22f14467x504"
image = signwriting_to_image(fsw,
line_color=(10, 23, 122, 255),
fill_color=(255, 255, 255, 0))
self.assert_image_equal_with_reference(fsw, image)

assert_array_equal(np.array(image), np.array(reference_image))
def test_image_with_line_and_embedded_color(self):
fsw = "M518x518S30a00482x483S33e00482x483"
image = signwriting_to_image(fsw, embedded_color=True,
line_color=(144, 70, 180, 255))
self.assert_image_equal_with_reference(fsw, image)

def test_image_with_fill_and_embedded_color(self):
fsw = "M518x533S20348482x515S10040493x485S20b00497x468S26600502x468"
image = signwriting_to_image(fsw, embedded_color=True,
fill_color=(123,234,0,255))
self.assert_image_equal_with_reference(fsw, image)


if __name__ == '__main__':
Expand Down
13 changes: 9 additions & 4 deletions signwriting/visualizer/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
from signwriting.formats.fsw_to_sign import fsw_to_sign
from signwriting.formats.fsw_to_swu import key2id, symbol_line, symbol_fill

# Type alias representing a tuple of four integers: Red, Green, Blue, Alpha
RGBA = Tuple[int, int, int, int]


@lru_cache(maxsize=None)
def get_font(font_name: str) -> ImageFont.FreeTypeFont:
Expand All @@ -23,8 +26,8 @@ def get_symbol_size(symbol: str):

# pylint: disable=too-many-locals, too-many-arguments
def signwriting_to_image(fsw: str, antialiasing=True, trust_box=True, embedded_color=False,
line_color: Tuple[int, int, int, int] = (0, 0, 0, 255),
fill_color: Tuple[int, int, int, int] = (255, 255, 255, 255)) -> Image:
line_color: RGBA = (0, 0, 0, 255),
fill_color: RGBA = (255, 255, 255, 255)) -> Image:
sign = fsw_to_sign(fsw)
if len(sign['symbols']) == 0:
return Image.new('RGBA', (1, 1), (0, 0, 0, 0))
Expand Down Expand Up @@ -55,7 +58,9 @@ def signwriting_to_image(fsw: str, antialiasing=True, trust_box=True, embedded_c
x, y = symbol["position"]
x, y = x - min_x, y - min_y
symbol_id = key2id(symbol["symbol"])
draw.text((x, y), symbol_fill(symbol_id), fill_color, fill_font, embedded_color=embedded_color)
draw.text((x, y), symbol_line(symbol_id), line_color, line_font, embedded_color=embedded_color)
draw.text((x, y), symbol_fill(symbol_id), fill=fill_color,
font=fill_font, embedded_color=embedded_color)
draw.text((x, y), symbol_line(symbol_id), fill=line_color,
font=line_font, embedded_color=embedded_color)

return img

0 comments on commit 29c0475

Please sign in to comment.