Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pyqt5 pylatex

import sys
import matplotlib as mpl
from matplotlib.backends.backend_agg import FigureCanvasAgg
from PySide import QtGui, QtCore

def mathTex_to_QPixmap(mathTex, fs):

    #---- set up a mpl figure instance ----

    fig = mpl.figure.Figure()
    fig.patch.set_facecolor('none')
    fig.set_canvas(FigureCanvasAgg(fig))
    renderer = fig.canvas.get_renderer()

    #---- plot the mathTex expression ----

    ax = fig.add_axes([0, 0, 1, 1])
    ax.axis('off')
    ax.patch.set_facecolor('none')
    t = ax.text(0, 0, mathTex, ha='left', va='bottom', fontsize=fs)

    #---- fit figure size to text artist ----

    fwidth, fheight = fig.get_size_inches()
    fig_bbox = fig.get_window_extent(renderer)

    text_bbox = t.get_window_extent(renderer)

    tight_fwidth = text_bbox.width * fwidth / fig_bbox.width
    tight_fheight = text_bbox.height * fheight / fig_bbox.height

    fig.set_size_inches(tight_fwidth, tight_fheight)

    #---- convert mpl figure to QPixmap ----

    buf, size = fig.canvas.print_to_buffer()
    qimage = QtGui.QImage.rgbSwapped(QtGui.QImage(buf, size[0], size[1],
                                                  QtGui.QImage.Format_ARGB32))
    qpixmap = QtGui.QPixmap(qimage)

    return qpixmap
Comment

PREVIOUS NEXT
Code Example
Python :: pyqt tex 
Python :: python watchgod 
Python :: python pdf to excel 
Python :: python download video from url requests 
Python :: python print dict new line 
Python :: load static files in django 
Python :: python how to copy a 2d array leaving out last column 
Python :: feet to meter python 
Python :: python install gimp 
Python :: how to delete a turtle in python 
Python :: display result in same page using flask api 
Python :: boto3 with aws profile 
Python :: text size legend to bottom matplotlib 
Python :: value_counts to list 
Python :: matplotlib show percentage y axis 
Python :: how to change a string to small letter in python 
Python :: plotly hide trace 
Python :: python live radio 
Python :: django all urls 
Python :: grassmann formula 
Python :: pil image base64 
Python :: how to find python version 
Python :: uninstall poetry 
Python :: PIL Make Circle 
Python :: plt normalized histogram 
Python :: python pil get pixel 
Python :: dataframe change specicf values in column 
Python :: dataframe sort by column 
Python :: python iterate over multidimensional dictionary 
Python :: how to convert an image to matrix in python 
ADD CONTENT
Topic
Content
Source link
Name
9+6 =