Skip to content

Instantly share code, notes, and snippets.

@jihyeonRyu
Last active February 7, 2020 05:17
Show Gist options
  • Save jihyeonRyu/8d7c3c8b909ab464d757dc4e2dc1c394 to your computer and use it in GitHub Desktop.
Save jihyeonRyu/8d7c3c8b909ab464d757dc4e2dc1c394 to your computer and use it in GitHub Desktop.
opencv automated fontsize
import cv2
def get_font_point_and_scale(text, x, y, h, w, thickness, font):
(text_width, text_height) = cv2.getTextSize(text, font, 1.0, thickness)[0]
scalex = w/text_width
scaley = h/text_height
scale = min(scalex, scaley)
marginx = 0 if scale == scalex else int(w *(scalex-scale)/scalex*0.5)
marginy = 0 if scale == scaley else int(h *(scaley-scale)/scaley*0.5)
return (x+marginx, y+h-marginy), scale
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment