From 29c047546345dc65d4b8a03ac28765eb2609421b Mon Sep 17 00:00:00 2001 From: Bipin Date: Wed, 10 Apr 2024 09:39:37 +0530 Subject: [PATCH] add more tests --- .../M518x517S10040482x485S26500493x474.png | Bin 0 -> 341 bytes .../M518x517S10043487x482S20500487x507.png | Bin 0 -> 1050 bytes .../M518x518S30a00482x483S33e00482x483.png | Bin 0 -> 2151 bytes ...10040493x485S20b00497x468S26600502x468.png | Bin 0 -> 446 bytes ...17e19460x481S22a07523x481S22a11463x481.png | Bin 0 -> 2553 bytes signwriting/visualizer/test_visualize.py | 63 +++++++++++------- signwriting/visualizer/visualize.py | 13 ++-- 7 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 signwriting/visualizer/test_assets/M518x517S10040482x485S26500493x474.png create mode 100644 signwriting/visualizer/test_assets/M518x517S10043487x482S20500487x507.png create mode 100644 signwriting/visualizer/test_assets/M518x518S30a00482x483S33e00482x483.png create mode 100644 signwriting/visualizer/test_assets/M518x533S20348482x515S10040493x485S20b00497x468S26600502x468.png create mode 100644 signwriting/visualizer/test_assets/M536x517S30a00482x482S33e00482x482S17e11516x464S17e19460x481S22a07523x481S22a11463x481.png diff --git a/signwriting/visualizer/test_assets/M518x517S10040482x485S26500493x474.png b/signwriting/visualizer/test_assets/M518x517S10040482x485S26500493x474.png new file mode 100644 index 0000000000000000000000000000000000000000..b7672cf8fd894b8fd37bf5ca0bffd93347559b20 GIT binary patch literal 341 zcmV-b0jmCqP)>g z<(gZcz+Mctm`q5M9050g^gOccq#i~W(j@!9E&2Vo1a@Pv$I*l|$qsN2Z2s|;k51}o z6rnyei2*M$*mh}F@n`Q%6Us42NRwE%Z`}DG4UNf`Q_eH+<`Lu~6obt?a-hN`YeG4i zP>v>)qY34xPRM11^`yRr&R!W@Q4H4iUpAQP8d{AXF|032lY0rjQe8t$C`S{@(S&kT nC$u^kr#=AtsIH+le&qNDK($!B8Dzug00000NkvXXu0mjf0f3GN literal 0 HcmV?d00001 diff --git a/signwriting/visualizer/test_assets/M518x517S10043487x482S20500487x507.png b/signwriting/visualizer/test_assets/M518x517S10043487x482S20500487x507.png new file mode 100644 index 0000000000000000000000000000000000000000..3f27214203da9de3bdf9f92db10721ae59dbe055 GIT binary patch literal 1050 zcmV+#1m*jQP)TANI(iIf)s5R>7t`mBS`5c#(>&QFt||*acUMK z1sS#KLJ@6|P)RJ(DrK0NOr%XabxKjus!d{|W;CyRzb@(-QkyiHsaH4s;Lm-Y^E=#o zIp+c*!ewP-WaOijm6f*>z5mkSfq{W806?Ko_+)u`x$??zygqSi|??4GYmtuwY7a74JP69`GJLng?BF%r)iqg($aG0-(V6R zj~ASsomE{5PSZ4}si|o^9;#NWznPnx`%WMb__-qoNzcCdUSNO z^_lQsFv#%S~1DS2>o zbo3G;f=;J1Mxqo&F(#8~J{pRMNYgaa+S62o@o`E;Ma8!OfZ5sEzwGYrz7}hc zy}iBq^8vW*cKbSk{eFL_uCC4o0GN}L^C(&$|LDx?^@_{O%fANz%*)IBX>V^YE#7z0 z*Vp$60H8!7aocP*+v(})Sxrri4FE7VH}~gQef;g<@bK{Eva+&!0Dy&sg+Ck|9J~?_ zCNUa~IsibiSbU^VC>#KQ`T6;d{r&w{W3_p@<954m78e)W008B3x!vh>N)v@ihK7bT z{ot zo);zwg+km!?gO$OE0xMMGKpMOz~}RcbUK~!gsB%-EJ>pMX#!=vcmb&X|Y(U0RXjH?d{Ia z&QJM#en2P`J~5lkwX94shzOIDlMQKUX?t+tpG>7v&2DXNy~)}PM|S4t=ij3!iXpI4 zsazvJjjZ8Lr&HS8+&l&Vn3|gUN8~m=J^lBYnVI)l!(}qrV;+we)M~Z2Js!^u0KlG} z9)s0tttu!e_z?hbe0;o#6?|-Ltl8mk4 z(V(U-RS_de9|&edL0~{Z_H97+ZDbgj{mt8M|7gZD;EaOy9KUn!AMbw4{k{9{yWjmT zh7f|`NJ~-A{K^}xA8XY5Y1V#f6w7cz#33GFfN&gRc|x4@^7Cj79Ot8&vSRcS#-GkDR4BH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+SM~(4*002;usEDSw` zN6CZF0030qZ2#zG0XR%V_0-6`)3t|bE92?PrtHV&KQI*Q9uO%l0D%9k?$_o7CL;v+t)mr*t{e+5)E_9l z-n6aYochb5z#tCslt;=>7bQR3(^l3y*OlG9iw%DQ0C;=Lgijv>&D}g<6VAiv`kc;H zhHgukGsiifMQ#rqCsp0M)bPimD-ZWR4aR0>yorOw-#4l>OC~LOHDUA{{{M0Hl9qm( zzcj?N_HjY!csx+Z2y`a)R+Fo|WV6s-sr&DxDP#mn`COINQrt6-CK-`qGA+LPaM#cI z5#3Fy`Kzn^Wq4!_iJPCs#L3fGU8^y|-anuWI{!&dm9E7Y86F*wy7<8B%bkAR>o3p7hQDs(YMjWrYjU|yp{ ztzk3#(n6<=%D{P;(}I#B8PD_UvujLRGT3j7q-6E6*;5_$PZWyA9x~+I`a5-ujTHq% z_!O@C$D1)PS|r+~r_nGuWv42evM>^9h-d8wr{+Wf4g*dQPAgZFmJGW1X-+w5q701WX2809VzYNf&Ay097kY2z1! dBn|h6_%F^px``FtiTD5j002ovPDHLkV1hhT9o+x` literal 0 HcmV?d00001 diff --git a/signwriting/visualizer/test_assets/M518x533S20348482x515S10040493x485S20b00497x468S26600502x468.png b/signwriting/visualizer/test_assets/M518x533S20348482x515S10040493x485S20b00497x468S26600502x468.png new file mode 100644 index 0000000000000000000000000000000000000000..e2732902717b5d435b269a4f959b5eff76f7c17d GIT binary patch literal 446 zcmV;v0YUzWP)bF5Qk?sAt8M*50WRau@ONLECgRbu(P|`DqSRSl|>*$npA1hIb1OQa^MNxF0W!bgzJBJdAU} z3MEN$+g<|zT9&o#N@^B~G0n29ZJy^>Y=i&+ZQK4VisC|R$yS@DojvxGavbNQEXz}^ zLg#BhA^Rugy6$^bRVP}6E}sCUJ-mFL=e^Z+eSGjS6a>MG>YEanVNZD(5k-3~gg~mV zy;s35A$CHOO18^xsZ@Ow>=H6U6xxM^5ZGtBw=6HAp%@_wBSc|@D2xyVJE7V3LwyLt z@Y(nMSJgA(=kGxq`}W!@*p*CsLPKGf(4av*WTW+)e>;XoP5aemfB(bL6qN>c2^k>@ zBSc|@DA)-NT3i>obO>mUYezBWgp7h+LdwNe=OWq#zl6F{P%-A6zOYM(ose?cQ-gXD o)7}VC7$FL?xKfzY4+?fd-zA4Cx5)Po2><{907*qoM6N<$f;(8hzW@LL literal 0 HcmV?d00001 diff --git a/signwriting/visualizer/test_assets/M536x517S30a00482x482S33e00482x482S17e11516x464S17e19460x481S22a07523x481S22a11463x481.png b/signwriting/visualizer/test_assets/M536x517S30a00482x482S33e00482x482S17e11516x464S17e19460x481S22a07523x481S22a11463x481.png new file mode 100644 index 0000000000000000000000000000000000000000..3fa2c5c25ea3e1ace2a098f34480fc6addd18106 GIT binary patch literal 2553 zcmV$(jh000TVNklk>ocK7V=?~1C@N@tUZv<038=BaA2QyzEzO#1SYhy;Koz`elRB9iIUwF2fL5fR`y z;4WK~2mDP$QlS+vKl(*-CU<(u`U0y(B&iiJ7m3Id7kq`AC*%|8a}V!&ss{lpq;=9a zBq9%|UBsC0zDZJI8lAP4>enW*ZriI2cJsAH7$71ufcd5S$v&`w{Qwe2aOffiTyh)v z^>|x)go(%`;A!VQm_Ws-<$RhwfGYrMs;FFk4`F}=GFqySoQgo(%~;P=!Ino8gfAg5g?9v}T073sO;092Jzwsa(WS|ng1 zG6MJm5Oft+10z)Rb;~m`r5kg{zK*73kp*zNjN>g1FcG;OSOLVlife(2cWeT&k{QVuzVE#d*L0Zszyd1gi+ z`&3px3T6~>xJ3*}5$Oy39Z2<5J*uh?MIavsyw`mo@#nqgn<29DfFqn8Sx<$L>j-=s zco8Ue%<6F(r~=NY>Y2#<_Dez2fJ{%>uT*uKyFf(RVD^i*0=EEH0^(vR z2j26d>g@&ZQwa<@-%-=Z2ulLixqSEyb@^uy7zg|rsPOT(95@Jk0&D~p)|kYQOXfrJKBt@Z>2Z)rTWBHjE=Vz%MaF zpslOwKfno$0}3Pq>6mD7=yTw=z-(2mA`FR0N8p$Pma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literal 0 HcmV?d00001 diff --git a/signwriting/visualizer/test_visualize.py b/signwriting/visualizer/test_visualize.py index a131a55..d1efe24 100644 --- a/signwriting/visualizer/test_visualize.py +++ b/signwriting/visualizer/test_visualize.py @@ -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" @@ -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__': diff --git a/signwriting/visualizer/visualize.py b/signwriting/visualizer/visualize.py index 6b9df38..0c926a2 100644 --- a/signwriting/visualizer/visualize.py +++ b/signwriting/visualizer/visualize.py @@ -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: @@ -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)) @@ -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