diff --git a/signwriting/visualizer/test_assets/AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S20544510x500S10019476x475.png b/signwriting/visualizer/test_assets/AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S20544510x500S10019476x475.png index 6b05f77..ebb8df6 100644 Binary files a/signwriting/visualizer/test_assets/AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S20544510x500S10019476x475.png and b/signwriting/visualizer/test_assets/AS10011S10019S2e704S2e748M525x535S2e748483x510S10011501x466S20544510x500S10019476x475.png differ diff --git a/signwriting/visualizer/test_assets/M528x526S1ce40506x474S1ce48472x474S22a04507x511S22a14480x510.png b/signwriting/visualizer/test_assets/M528x526S1ce40506x474S1ce48472x474S22a04507x511S22a14480x510.png new file mode 100644 index 0000000..45a25c4 Binary files /dev/null and b/signwriting/visualizer/test_assets/M528x526S1ce40506x474S1ce48472x474S22a04507x511S22a14480x510.png differ diff --git a/signwriting/visualizer/test_visualize.py b/signwriting/visualizer/test_visualize.py index 6887442..1da025a 100644 --- a/signwriting/visualizer/test_visualize.py +++ b/signwriting/visualizer/test_visualize.py @@ -19,6 +19,16 @@ def test_image_fsw(self): self.assertEqual(reference_image.size, image.size) assert_array_equal(np.array(image), np.array(reference_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)) + def test_image_invalid_fsw(self): fsw = "S20555" image = signwriting_to_image(fsw) diff --git a/signwriting/visualizer/visualize.py b/signwriting/visualizer/visualize.py index 457f73b..b22a4bd 100644 --- a/signwriting/visualizer/visualize.py +++ b/signwriting/visualizer/visualize.py @@ -13,7 +13,8 @@ def get_font(font_name: str): return ImageFont.truetype(str(font_path), 30) -def signwriting_to_image(fsw: str) -> Image: +# pylint: disable=too-many-locals +def signwriting_to_image(fsw: str, antialiasing=True) -> Image: sign = fsw_to_sign(fsw) if len(sign['symbols']) == 0: return Image.new('RGBA', (1, 1), (0, 0, 0, 0)) @@ -24,15 +25,16 @@ def signwriting_to_image(fsw: str) -> Image: max_x, max_y, = sign["box"]["position"] img = Image.new('RGBA', (max_x - min_x, max_y - min_y), (255, 255, 255, 0)) draw = ImageDraw.Draw(img) + draw.fontmode = 'L' if antialiasing else '1' - line_font = get_font('SuttonSignWritingLine') fill_font = get_font('SuttonSignWritingFill') + line_font = get_font('SuttonSignWritingLine') for symbol in sign['symbols']: x, y = symbol["position"] x, y = x - min_x, y - min_y symbol_id = key2id(symbol["symbol"]) - draw.text((x, y), symbol_line(symbol_id), font=line_font, fill=(0, 0, 0)) draw.text((x, y), symbol_fill(symbol_id), font=fill_font, fill=(255, 255, 255)) + draw.text((x, y), symbol_line(symbol_id), font=line_font, fill=(0, 0, 0)) return img