Skip to content

Instantly share code, notes, and snippets.

@jihyeonRyu
jihyeonRyu / auto_font_size.py
Last active February 7, 2020 05:17
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)