Skip to content

Commit

Permalink
fix(visualizer): draw fill before line
Browse files Browse the repository at this point in the history
  • Loading branch information
AmitMY committed Jan 7, 2024
1 parent bf6d34f commit 418792d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 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.
10 changes: 10 additions & 0 deletions signwriting/visualizer/test_visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions signwriting/visualizer/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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

0 comments on commit 418792d

Please sign in to comment.