Skip to content

Commit 408c901

Browse files
committed
Add in font downloader
1 parent 0000fc2 commit 408c901

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

surya/postprocessing/text.py

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import os
2+
3+
import requests
14
from PIL import Image, ImageDraw, ImageFont
25
from surya.settings import settings
36

@@ -21,6 +24,14 @@ def draw_text_on_image(bboxes, texts, image_size=(1024, 1024), font_path=setting
2124

2225
# Shrink the text to fit in the bbox if needed
2326
box_font_size = font_size
27+
28+
# Download font if it doesn't exist
29+
if not os.path.exists(font_path):
30+
with requests.get(settings.FONT_DL_PATH, stream=True) as r, open(font_path, 'wb') as f:
31+
r.raise_for_status()
32+
for chunk in r.iter_content(chunk_size=8192):
33+
f.write(chunk)
34+
2435
font = ImageFont.truetype(font_path, box_font_size)
2536
text_width, text_height = get_text_size(text, font)
2637
while (text_width > bbox_width or text_height > bbox_height) and box_font_size > 6:

surya/settings.py

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Settings(BaseSettings):
1717
RESULT_DIR: str = "results"
1818
BASE_DIR: str = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1919
FONT_DIR: str = os.path.join(BASE_DIR, "static", "fonts")
20+
FONT_DL_PATH: str = "https://github.com/satbyy/go-noto-universal/releases/download/v7.0/GoNotoKurrent-Regular.ttf"
2021

2122
@computed_field
2223
def TORCH_DEVICE_MODEL(self) -> str:

0 commit comments

Comments
 (0)