From 0fdc36f0080df31b10fab06517a02516bfc2d58e Mon Sep 17 00:00:00 2001 From: Amit Moryossef Date: Wed, 31 Jan 2024 09:45:28 +0100 Subject: [PATCH] feat(visualize): add option to not trust the box --- ...2e300413x552S22b04418x565S36520420x523.png | Bin 0 -> 2537 bytes signwriting/visualizer/test_visualize.py | 16 +++++++++++++ signwriting/visualizer/visualize.py | 21 ++++++++++++++++-- 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 signwriting/visualizer/test_assets/M500x500S2ff00407x501S1ce20436x535S2e300413x552S22b04418x565S36520420x523.png diff --git a/signwriting/visualizer/test_assets/M500x500S2ff00407x501S1ce20436x535S2e300413x552S22b04418x565S36520420x523.png b/signwriting/visualizer/test_assets/M500x500S2ff00407x501S1ce20436x535S2e300413x552S22b04418x565S36520420x523.png new file mode 100644 index 0000000000000000000000000000000000000000..5b75fde23c73203168671e2a11f67ba94e9c4a9c GIT binary patch literal 2537 zcmV{~r000TFNklKw!uPb5v3BLNoh$c8ugEc5F$ZU zkfK&iRSKyi{K~M;eo)aWKTZY-4=*QQHCAU;mg{dpXW`x4Utp+JIBQd7u>#U^Fn^Y>z^?6N^K&F^GFTnf2e8A_p0vH860bDh@HUlI34F)Iy znt|)U(?F8viXa2{qXBvw$V@m4@OAWHz6#8Ydxd)3-2f_q(FubA?g3iRPo;Yj(G^TR z^MFp^Z@{p)!vHD3yQY%+5(Q$bZ<*!>* zbNr7NB|hK?x`{nK7Cp*0%<+I1B|c!k`8+??LP{xd@;L^12sk4m=Ny!|MSLPs3A_U= zP)enEQRYUJQnP>p;7^{F8CU!f^yB%l7+rU9@;QpGeLx406R-J9GI}mk4`lQg4Dg`2 ziJRgvpBZLqy`hwP9C!o0*0m^Fiz=m7NBbP5d^+%Za|54_rA$-_xl_-h@6;2~S|*kh zq5N?4Vuk>v@o1HvZf2`w1bR8O8(12zO2UA=WI&#atL`wBr2{8SWzQ0a1wfnbG3I6o zunDu)oiG?vX&UgRsq_dQ*U-?@^Y78S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$gim=NY@JN~ZVQf%~fa=oH z(!D!(?)+_5R#tl`OlxavyLp;@g5G!R7ZArV1}GU=fc9)}88fxDwbu5-h5iH)aS=OX z7Zc;+57AL)_8%{b$X%|LyO5!@rx{f~g&C~!Da>G%|0@_ry8ue5iRk~yxT$%K% zL;v`p4VW_!AR->80CrJIEdYWd^4GX1bRqu(&ux^M ImageFont.FreeTypeFont: font_path = Path(__file__).parent / f'{font_name}.ttf' return ImageFont.truetype(str(font_path), 30) +def get_symbol_size(symbol: str): + font = get_font('SuttonSignWritingLine') + line_id = symbol_line(key2id(symbol)) + left, top, right, bottom = font.getbbox(line_id) + return right - left, bottom - top + + # pylint: disable=too-many-locals -def signwriting_to_image(fsw: str, antialiasing=True) -> Image: +def signwriting_to_image(fsw: str, antialiasing=True, trust_box=True) -> Image: sign = fsw_to_sign(fsw) if len(sign['symbols']) == 0: return Image.new('RGBA', (1, 1), (0, 0, 0, 0)) @@ -22,7 +29,17 @@ def signwriting_to_image(fsw: str, antialiasing=True) -> Image: positions = [s["position"] for s in sign['symbols']] min_x = min(positions, key=lambda p: p[0])[0] min_y = min(positions, key=lambda p: p[1])[1] + max_x, max_y, = sign["box"]["position"] + + if not trust_box: + max_x, max_y = 0, 0 + for symbol in sign['symbols']: + symbol_x, symbol_y = symbol["position"] + symbol_width, symbol_height = get_symbol_size(symbol["symbol"]) + max_x = max(max_x, symbol_x + symbol_width) + max_y = max(max_y, symbol_y + symbol_height) + img = Image.new('RGBA', (max_x - min_x, max_y - min_y), (255, 255, 255, 0)) draw = ImageDraw.Draw(img) if not antialiasing: