Skip to content

Commit

Permalink
Fix caption overflow (#899)
Browse files Browse the repository at this point in the history
* Fix caption overflow

* Small cleaup

* remove logging import
  • Loading branch information
joeyballentine committed Sep 5, 2022
1 parent 1316444 commit 6a90a0e
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions backend/src/nodes/utils/pil_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ def rotate(

def add_caption(img: np.ndarray, caption: str, size: int, position: str) -> np.ndarray:
"""Add caption with PIL"""
fontsize = round(size*0.8)
if position == 'bottom':
img = cv2.copyMakeBorder(img, 0, size, 0, 0, cv2.BORDER_CONSTANT, value=(0, 0, 0, 1))
elif position == 'top':
img = cv2.copyMakeBorder(img, size, 0, 0, 0, cv2.BORDER_CONSTANT, value=(0, 0, 0, 1))
fontsize = round(size * 0.8)
if position == "bottom":
img = cv2.copyMakeBorder(
img, 0, size, 0, 0, cv2.BORDER_CONSTANT, value=(0, 0, 0, 1)
)
elif position == "top":
img = cv2.copyMakeBorder(
img, size, 0, 0, 0, cv2.BORDER_CONSTANT, value=(0, 0, 0, 1)
)
else:
raise RuntimeError(f"Unknown position {position}")

Expand All @@ -88,12 +92,17 @@ def add_caption(img: np.ndarray, caption: str, size: int, position: str) -> np.n
font = ImageFont.truetype(font_path, fontsize)
h, w, c = get_h_w_c(img)
text_x = w // 2
if position == 'bottom':
text_y = h - round(size/2)
elif position == 'top':
text_y = round(size/2)
if position == "bottom":
text_y = h - round(size / 2)
elif position == "top":
text_y = round(size / 2)
font_color = (255,) * c

fw, _ = font.getsize(caption)
# scale font size to fit image
if fw > w:
font = ImageFont.truetype(font_path, round(fontsize * w / fw))

d = ImageDraw.Draw(pimg)
d.text(
(text_x, text_y),
Expand Down

0 comments on commit 6a90a0e

Please sign in to comment.