Skip to content

Commit 3a73694

Browse files
committed
Use Pillow >= 10.1 default font_size support in utils.draw_bounding_boxes
1 parent 3e56edb commit 3a73694

5 files changed

Lines changed: 38 additions & 4 deletions

File tree

1.05 KB
Loading
1.25 KB
Loading
855 Bytes
Loading

test/test_utils.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,21 @@ def test_draw_boxes_with_coloured_label_text_boxes():
193193
assert_equal(result, expected)
194194

195195

196+
@pytest.mark.skipif(PILLOW_VERSION < (10, 1), reason="The reference image is only valid for PIL >= 10.1")
197+
@pytest.mark.parametrize("font_size", [None, 15, 20])
198+
def test_draw_boxes_with_default_font_size(font_size):
199+
img = torch.full((3, 100, 100), 255, dtype=torch.uint8)
200+
labels = ["a", "b", "c", "d"]
201+
colors = ["green", "#FF00FF", (0, 255, 0), "red"]
202+
result = utils.draw_bounding_boxes(img, boxes, labels=labels, colors=colors, fill=True, font_size=font_size)
203+
204+
path = os.path.join(
205+
os.path.dirname(os.path.abspath(__file__)), "assets", "fakedata", f"draw_boxes_default_font_size{font_size}.png"
206+
)
207+
expected = torch.as_tensor(np.array(Image.open(path))).permute(2, 0, 1)
208+
assert_equal(result, expected)
209+
210+
196211
@pytest.mark.skipif(PILLOW_VERSION < (10, 1), reason="The reference image is only valid for PIL >= 10.1")
197212
def test_draw_rotated_boxes():
198213
img = torch.full((3, 500, 500), 255, dtype=torch.uint8)
@@ -289,10 +304,15 @@ def test_draw_invalid_boxes():
289304
utils.draw_bounding_boxes(img_correct, boxes_wrong)
290305

291306

307+
@pytest.mark.skipif(PILLOW_VERSION >= (10, 1), reason="The warning is only valid for PIL < 10.1")
292308
def test_draw_boxes_warning():
293309
img = torch.full((3, 100, 100), 255, dtype=torch.uint8)
294310

295-
with pytest.warns(UserWarning, match=re.escape("Argument 'font_size' will be ignored since 'font' is not set.")):
311+
warning_txt = (
312+
"Argument 'font_size' will be ignored since 'font' is not set. "
313+
"To use 'font_size' with the default font, please upgrade to Pillow >= 10.1."
314+
)
315+
with pytest.warns(UserWarning, match=re.escape(warning_txt)):
296316
utils.draw_bounding_boxes(img, boxes, font_size=11)
297317

298318

torchvision/utils.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,23 @@ def draw_bounding_boxes(
371371
label_background_colors = colors.copy() # type: ignore[assignment]
372372

373373
if font is None:
374-
if font_size is not None:
375-
warnings.warn("Argument 'font_size' will be ignored since 'font' is not set.")
376-
txt_font = ImageFont.load_default()
374+
# This may throw if the version string is from an install that comes from a
375+
# non-stable or development version. We'll fall back to the old behavior in
376+
# such cases.
377+
try:
378+
PILLOW_VERSION = tuple(int(x) for x in PILLOW_VERSION_STRING.split("."))
379+
except Exception:
380+
PILLOW_VERSION = None
381+
382+
if PILLOW_VERSION is None or PILLOW_VERSION < (10, 1):
383+
if font_size is not None:
384+
warnings.warn(
385+
"Argument 'font_size' will be ignored since 'font' is not set. "
386+
"To use 'font_size' with the default font, please upgrade to Pillow >= 10.1."
387+
)
388+
txt_font = ImageFont.load_default()
389+
else:
390+
txt_font = ImageFont.load_default(size=font_size)
377391
else:
378392
txt_font = ImageFont.truetype(font=font, size=font_size or 10)
379393

0 commit comments

Comments
 (0)