matplotlib:获取文本边界框,独立于后端

时间:2014-03-26 16:24:09

标签: python matplotlib

我想在matplotlib图中的某些文本周围获取边界框(尺寸)。帖子here帮助我意识到我可以使用方法text.get_window_extent(renderer)来获取边界框,但我必须提供正确的渲染器。有些后端没有方法figure.canvas.get_renderer(),所以我尝试matplotlib.backend_bases.RendererBase()来获取渲染器并且它没有产生令人满意的结果。这是一个简单的例子

import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

fig = plt.figure()
ax = plt.subplot()
txt = fig.text(0.15,0.5,'afdjsklhvvhwd', fontsize = 36)
renderer1 = fig.canvas.get_renderer()
renderer2 = mpl.backend_bases.RendererBase()
bbox1 = txt.get_window_extent(renderer1)
bbox2 = txt.get_window_extent(renderer2)
rect1 = Rectangle([bbox1.x0, bbox1.y0], bbox1.width, bbox1.height, \
    color = [0,0,0], fill = False)
rect2 = Rectangle([bbox2.x0, bbox2.y0], bbox2.width, bbox2.height, \
    color = [1,0,0], fill = False)
fig.patches.append(rect1)
fig.patches.append(rect2)
plt.draw()

这会产生以下情节:

image

显然红色框太小了。我认为保罗的答案here发现了同样的问题。黑匣子看起来很棒,但我不能使用MacOSX后端,或任何其他没有方法figure.canvas.get_renderer()的人。

如果重要,我使用的是Mac OS X 10.8.5,Matplotlib 1.3.0和Python 2.7.5

2 个答案:

答案 0 :(得分:7)

这是我的解决方案/黑客。 @tcaswell建议我看一下matplotlib如何使用紧密的边界框来处理保存数字。我在Github上为backend_bases.py找到the code,它将图形保存到临时文件对象,只是为了从缓存中获取渲染器。我将这个技巧变成了一个小函数,如果它存在于后端,则使用内置方法get_renderer(),否则使用save方法。

def find_renderer(fig):

    if hasattr(fig.canvas, "get_renderer"):
        #Some backends, such as TkAgg, have the get_renderer method, which 
        #makes this easy.
        renderer = fig.canvas.get_renderer()
    else:
        #Other backends do not have the get_renderer method, so we have a work 
        #around to find the renderer.  Print the figure to a temporary file 
        #object, and then grab the renderer that was used.
        #(I stole this trick from the matplotlib backend_bases.py 
        #print_figure() method.)
        import io
        fig.canvas.print_pdf(io.BytesIO())
        renderer = fig._cachedRenderer
    return(renderer)

以下是使用find_renderer()的结果,以及我原始示例中稍微修改过的代码版本。使用具有get_renderer()方法的TkAgg后端,我得到:

TkAgg

使用没有get_renderer()方法的MacOSX后端,我得到:

MacOSX

显然,使用MacOSX后端的边界框并不完美,但它比我原来问题中的红框好得多。

答案 1 :(得分:0)

如果您想获得旋转文本区域的紧密边界框,可以采用以下解决方法。

# generate text layer
def text_on_canvas(text, myf, ro, margin = 1):
    axis_lim = 1

    fig = plt.figure(figsize = (5,5), dpi=100)
    plt.axis([0, axis_lim, 0, axis_lim])

    # place the left bottom corner at (axis_lim/20,axis_lim/20) to avoid clip during rotation
    aa = plt.text(axis_lim/20.,axis_lim/20., text, ha='left', va = 'top', fontproperties = myf, rotation = ro, wrap=True)
    plt.axis('off')
    text_layer = fig2img(fig) # convert to image
    plt.close()

    we = aa.get_window_extent()
    min_x, min_y, max_x, max_y = we.xmin, 500 - we.ymax, we.xmax, 500 - we.ymin
    box = (min_x-margin, min_y-margin, max_x+margin, max_y+margin)

    # return coordinates to further calculate the bbox of rotated text
    return text_layer, min_x, min_y, max_x, max_y 


def geneText(text, font_family, font_size, style):
    myf = font_manager.FontProperties(fname=font_family, size=font_size)
    ro = 0

    if style < 8: # rotated text
        # no rotation, just to get the minimum bbox
        htext_layer, min_x, min_y, max_x, max_y = text_on_canvas(text, myf, 0)

        # actual rotated text
        ro = random.randint(0, 90)
        M = cv2.getRotationMatrix2D((min_x,min_y),ro,1)
        # pts is 4x3 matrix
        pts = np.array([[min_x, min_y, 1],[max_x, min_y, 1],[max_x, max_y, 1],[min_x, max_y,1]]) # clockwise
        affine_pts = np.dot(M, pts.T).T
        #print affine_pts
        text_layer, _, _, _, _ = text_on_canvas(text, myf, ro)

        visualize_points(htext_layer, pts)
        visualize_points(text_layer, affine_pts)

        return text_layer  

    else:
        raise NotImplementedError


fonts = glob.glob(fonts_path + '/*.ttf')
ret = geneText('aaaaaa', fonts[0], 80, 1)

结果如下所示:第一个是未旋转的,第二个是旋转的文本区域。完整的代码段为here

enter image description here enter image description here