File size: 1,202 Bytes
087643a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Shared by prepare_dataset.py's label check and mine_failures.py's
# failure renders. PIL's bitmap default font (~10px) turns into an
# illegible smear once resized for a notebook or memo screenshot - which
# is the only place either render gets looked at.
from __future__ import annotations


def load_label_font(size: int):
    """Finds a real scalable font. matplotlib ships its own DejaVu copy
    and is already a pinned dependency, so that's guaranteed to exist -
    a bare "DejaVuSans-Bold.ttf" only works if the OS's font search
    happens to resolve it, which isn't something to bet a Kaggle run on."""
    from PIL import ImageFont

    candidates = []

    try:
        import matplotlib

        candidates.append(f"{matplotlib.get_data_path()}/fonts/ttf/DejaVuSans-Bold.ttf")
    except Exception:
        pass

    candidates += [
        "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",  # Debian/Ubuntu, Kaggle
        "DejaVuSans-Bold.ttf",  # works if OS font search resolves it (e.g. Windows)
    ]

    for path in candidates:
        try:
            return ImageFont.truetype(path, size)
        except OSError:
            continue

    return ImageFont.load_default()