Python 3和二分函数来检查字体的大小

时间:2015-11-29 04:31:26

标签: python algorithm fonts

我有以下功能来检查font size是否小到足以容纳imagexy

from PIL import ImageFont                                                       

def fit(width, height, font, size, text):                                       
    font = ImageFont.truetype(font, size)                                       
    text_w, text_h = font.getsize(text)                                         
    text_area = text_w*text_h                                                   
    total_area = width*height                                                   
    if total_area>text_area:                                                    
        return True                                                             
    else:                                                                       
        return False                                                            



font = 'font.ttf'                                                               
counter = 0                                                                     
size = 60                                                                       
text = """                                                                      
The module is called bisect because it uses a basic bisection algorithm to do its w
"""                                                                             
while True:                                                                     
    counter += 1                                                                
    ans = fit(500, 500, font, size, text)                                       
    if ans == True:                                                             
        break                                                                   
    else:                                                                       
        size -=1                                                                
print("Font_Size: {}".format(size))                                             
print("Repetitions: {} ".format(counter))    

好吧,这些代码作为图像的参数widthheight并计算总面积并与文本的总面积进行比较,如果它适合(total area > total area text)则返回真正。我想做的是以下几点:

我想要适合该地区的最大font size

我们可以通过for循环实现这一点,从高值开始并在每次迭代中减去1。它有效,但速度很慢。

我想知道是否有任何方法可以通过Python实现的二分算法的内置函数来实现这一点,或者你有没有其他想法?

我使用的字体如下:https://www.google.com/fonts#QuickUsePlace:quickUse/Family:Open+Sans

以上代码的输出是: Font_Size: 49 Repetitions: 12

1 个答案:

答案 0 :(得分:0)

给出两个整数a<b和从整数到布尔(f)的单调函数True,False,其中f(a) = Truef(b) = False可以找到最大值m[a,b[的整数f(m) = Truedef findMaxOk(low_ok, high_notOk, f): print "find max. ok in [%d,%d[" % (low_ok, high_notOk) if low_ok+1 >= high_notOk: return low_ok mid = (low_ok + high_notOk) // 2 if f(mid): return findMaxOk(mid, high_notOk, f) else: return findMaxOk(low_ok, mid, f) 具有以下函数:

def maxFittingFontSize(width, height, font, text):
  f = lambda size: fit(width, height, font, size, text)
  return findMaxOk(1, 100, f)

要找到最大字体大小,您可以定义如下函数:

fit

顺便说一下:你不想分别在你的函数def fit(width, height, font, size, text): font = ImageFont.truetype(font, size) text_w, text_h = font.getsize(text) return text_w<width and text_h<height 宽度和高度上进行比较吗?像:

HTML