Exemplo n.º 1
0
def img_from_tex(tex, font_size=20):

    figure = matplotlib.figure.Figure(None, facecolor='white')
    #canvas = matplotlib.backends.backend_wxagg.FigureCanvasWxAgg(None, -1, figure)
    canvas = matplotlib.backends.backend_wxagg.FigureCanvasAgg(figure)

    font_size = font_size

    tex = "${0}$".format(tex)

    figure.clear()
    figure.text(0.05, 0.5, tex, size=font_size)
    canvas.draw()

    filename = 'equ.png'

    figure.savefig(filename, dpi=600)

    img = Image.open(filename)

    imginv = ImageChops.invert(img.convert('L'))

    box = imginv.getbbox()
    img = img.crop(box)

    img = ImageOps.expand(img, border=10, fill=(255, 255, 255))

    img.save(filename)

    return img
Exemplo n.º 2
0
def tex2img(tex, filename="equ.png", font_size=20):

    font_size = 20

    figure = matplotlib.figure.Figure(None, facecolor=(0, 0, 0, 0))
    canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(figure)

    font_size = font_size

    tex = "${0}$".format(tex)

    figure.clear()
    figure.text(0.05, 0.5, tex, size=font_size)

    canvas.draw()

    png = "equ.png"

    figure.savefig(png, transparent=True, dpi=300, format="png")

    img = Image.open(png)

    imginv = ImageChops.invert(img.convert('L'))

    box = imginv.getbbox()

    img = img.crop(box)

    img = ImageOps.expand(img, border=10, fill=(255, 255, 255, 0))

    img.save(png, format="png")

    return img