Skip to content

Commit c8c0f66

Browse files
authored
Fix #5 and add more tests (#7)
1 parent 824fa53 commit c8c0f66

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

image_dataset_viz/dataset_exporter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def get_default_font(text_size):
2525
try:
2626
return ImageFont.truetype(font="DejaVuSans.ttf", size=text_size)
2727
except OSError:
28-
font_path = Path(__file__).parent() / "fonts" / "DejaVuSans.ttf"
28+
font_path = Path(__file__).parent / "fonts" / "DejaVuSans.ttf"
2929
return ImageFont.truetype(font=font_path.as_posix(), size=text_size)
3030

3131

tests/test_dataset_exporter.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111
from unittest import TestCase, main
1212

1313
import numpy as np
14-
from PIL import Image
14+
from PIL import Image, ImageFont
1515

16-
from image_dataset_viz.dataset_exporter import resize_image, DatasetExporter
16+
from image_dataset_viz.dataset_exporter import resize_image, DatasetExporter, \
17+
get_default_font, to_pil, render_datapoint
1718

1819

1920
class TestDatasetExporter(TestCase):
@@ -24,6 +25,10 @@ def setUp(self):
2425
def tearDown(self):
2526
pass
2627

28+
def test_get_default_font(self):
29+
font = get_default_font(10)
30+
assert isinstance(font, ImageFont.FreeTypeFont)
31+
2732
def test_resize_image(self):
2833
size = (320, 300)
2934
large_img = Image.new('RGB', size=size)
@@ -47,6 +52,20 @@ def read_img(i):
4752
de = DatasetExporter(read_img_fn=read_img)
4853
de.export_datapoint(0, 0, "test.png")
4954

55+
def test_render_datapoint(self):
56+
57+
img = np.ones((100, 120, 3), dtype=np.uint8)
58+
res = render_datapoint(img, "test label", text_color=(0, 255, 0), text_size=10)
59+
assert isinstance(res, Image.Image)
60+
61+
target = Image.fromarray(np.ones((100, 120, 3), dtype=np.uint8))
62+
res = render_datapoint(img, target, text_color=(0, 255, 0), text_size=10)
63+
assert isinstance(res, Image.Image)
64+
65+
target = np.array([[10, 10], [55, 10], [55, 77], [10, 77]])
66+
res = render_datapoint(img, target, geom_color=(255, 0, 0))
67+
assert isinstance(res, Image.Image)
68+
5069
def test_export_datapoint(self):
5170

5271
def read_img(i):
@@ -128,6 +147,15 @@ def read_img(i):
128147
out_img = Image.open(fp)
129148
self.assertEqual(out_img.size, ((s + m) * n_cols, (s + m) * max_n_rows))
130149

150+
def test_to_pil(self):
151+
img = np.ones((100, 120, 3), dtype=np.uint8)
152+
pil_img = to_pil(img)
153+
assert isinstance(pil_img, Image.Image)
154+
155+
img = Image.fromarray(np.ones((100, 120, 3), dtype=np.uint8))
156+
pil_img = to_pil(img)
157+
assert isinstance(pil_img, Image.Image)
158+
131159

132160
if __name__ == "__main__":
133161
main()

0 commit comments

Comments
 (0)