Converting latex code to Images (or other displayble format) with Python

前端 未结 7 1123
谎友^
谎友^ 2020-12-05 07:42

I have a function I am consuming that returns a string of latex code. I need to generate an image from this. Most of the methods I have seen for doing so suggest calling a

7条回答
  •  半阙折子戏
    2020-12-05 08:06

    This is a bit ugly solution I often use, but I found it easiest to use in many cases.

    import matplotlib.pyplot as plt
    import io
    from PIL import Image, ImageChops
    
    white = (255, 255, 255, 255)
    
    def latex_to_img(tex):
        buf = io.BytesIO()
        plt.rc('text', usetex=True)
        plt.rc('font', family='serif')
        plt.axis('off')
        plt.text(0.05, 0.5, f'${tex}$', size=40)
        plt.savefig(buf, format='png')
        plt.close()
    
        im = Image.open(buf)
        bg = Image.new(im.mode, im.size, white)
        diff = ImageChops.difference(im, bg)
        diff = ImageChops.add(diff, diff, 2.0, -100)
        bbox = diff.getbbox()
        return im.crop(bbox)
    
    latex_to_img(r'\frac{x}{y^2}').save('img.png')
    

    Keep in mind, it requires pillow and matplotlib.

    pip install matplotlib pillow
    

提交回复
热议问题