Repository: pytorch
Issue: `add_image_with_boxes` method from `torch.utils.tensorboard.writer.SummaryWriter` is broken
Labels: triaged,module: tensorboard,oncall: visualization

### 🐛 Describe the bug

The method `add_image_with_boxes` of pytorch's SummaryWriter gets an error when `labels` option is not None

```python
import torch
from torch.utils.tensorboard.writer import SummaryWriter
writer = SummaryWriter()

image = torch.randn(3,100,100)
bbox = torch.tensor([[0, 1, 0, 1]])

writer.add_image_with_boxes(
    "test",
    image,
    box_tensor=bbox,
    labels=["test label"],
)
```

Will have the following error :

```
Traceback (most recent call last):
  File "/home/clementpinard/workspace/test.py", line 8, in <module>
    writer.add_image_with_boxes(
  File "/home/clementpinard/workspace/.venv/lib/python3.10/site-packages/torch/utils/tensorboard/writer.py", line 713, in add_image_with_boxes
    image_boxes(
  File "/home/clementpinard/workspace/.venv/lib/python3.10/site-packages/torch/utils/tensorboard/summary.py", line 457, in image_boxes
    image = make_image(
  File "/home/clementpinard/workspace/.venv/lib/python3.10/site-packages/torch/utils/tensorboard/summary.py", line 489, in make_image
    image = draw_boxes(image, rois, labels=labels)
  File "/home/clementpinard/workspace/.venv/lib/python3.10/site-packages/torch/utils/tensorboard/summary.py", line 468, in draw_boxes
    disp_image = _draw_single_box(
  File "/home/clementpinard/workspace/.venv/lib/python3.10/site-packages/torch/utils/tensorboard/summary.py", line 57, in _draw_single_box
    text_width, text_height = font.getsize(display_str)
AttributeError: 'ImageFont' object has no attribute 'getsize'
```

This is most likely a deprecation from pillow that was not detected. Applying this simple fix repairs the function in torch/utils/tensorboard/summary.py

```diff
diff --git a/torch/utils/tensorboard/summary.py b/torch/utils/tensorboard/summary.py
index e07d2c6b880..bf3b9820e21 100644
--- a/torch/utils/tensorboard/summary.py
+++ b/torch/utils/tensorboard/summary.py
@@ -108,7 +108,7 @@ def _draw_single_box(
     if display_str:
         text_bottom = bottom
         # Reverse list and print from bottom to top.
-        text_width, text_height = font.getsize(display_str)
+        text_width, text_height = font.font.getsize(display_str)
         margin = np.ceil(0.05 * text_height)
         draw.rectangle(
             [
```
I am not sure since when this call is deprecated though, I suppose a more clever fix would be to try both so that a former version of Pillow still works. I can do the corresponding PR if you want.

Since this Issue is very small I would also like to take this opportunity to formulate a related feature request : as you might see here https://github.com/pytorch/pytorch/blob/main/torch/utils/tensorboard/summary.py#L583 the bounding box will always be red. It would be nice to be able to specify a list of colors the same way we specify a list of labels to better read them. This would mean having a "colors" option in the method `add_image_with_boxes`, but also in the functions `image_boxes`, `make_image` and `draw_boxes`, and maybe a clever way to decide if we write the text in black or in white depending on the color. Nothing too complex actually.

### Versions

I don't believe it's really necessary, but here it goes :

```
PyTorch version: N/A
Is debug build: N/A
CUDA used to build PyTorch: N/A
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.2 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.27.0
Libc version: glibc-2.35

Python version: 3.11.4 (main, Jul  5 2023, 13:45:01) [GCC 11.2.0] (64-bit runtime)
Python platform: Linux-5.15.0-78-generic-x86_64-with-glibc2.35
Is CUDA available: N/A
CUDA runtime version: 12.2.128
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: 
GPU 0: NVIDIA RTX A6000
GPU 1: NVIDIA RTX A6000

Nvidia driver version: 535.86.10
cuDNN version: Probably one of the following:
/usr/lib/x86_64-linux-gnu/libcudnn.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_adv_infer.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_adv_train.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_infer.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_cnn_train.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_ops_infer.so.8.9.3
/usr/lib/x86_64-linux-gnu/libcudnn_ops_train.so.8.9.3
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: N/A

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Address sizes:                   52 bits physical, 57 bits virtual
Byte Order:                      Little Endian
CPU(s):                          64
On-line CPU(s) list:             0-63
Vendor ID:                       GenuineIntel
Model name:                      Intel(R) Xeon(R) Gold 6426Y
CPU family:                      6
Model:                           143
Thread(s) per core:              2
Core(s) per socket:              16
Socket(s):                       2
Stepping:                        8
CPU max MHz:                     4100.0000
CPU min MHz:                     800.0000
BogoMIPS:                        5000.00
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cat_l2 cdp_l3 invpcid_single intel_ppin cdp_l2 ssbd mba ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect avx_vnni avx512_bf16 wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke waitpkg avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid bus_lock_detect cldemote movdiri movdir64b enqcmd fsrm md_clear serialize tsxldtrk pconfig arch_lbr amx_bf16 avx512_fp16 amx_tile amx_int8 flush_l1d arch_capabilities
Virtualization:                  VT-x
L1d cache:                       1.5 MiB (32 instances)
L1i cache:                       1 MiB (32 instances)
L2 cache:                        64 MiB (32 instances)
L3 cache:                        75 MiB (2 instances)
NUMA node(s):                    2
NUMA node0 CPU(s):               0-15,32-47
NUMA node1 CPU(s):               16-31,48-63
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Not affected
Vulnerability Retbleed:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] numpy==1.25.1
[pip3] torch==2.0.1
[pip3] torchaudio==2.0.2
[pip3] torchvision==0.15.2
[pip3] triton==2.0.0
[conda] numpy                     1.25.1                   pypi_0    pypi
[conda] torch                     2.0.1                    pypi_0    pypi
[conda] torchaudio                2.0.2                    pypi_0    pypi
[conda] torchvision               0.15.2                   pypi_0    pypi
[conda] triton                    2.0.0                    pypi_0    pypi
```