Last active
April 16, 2016 02:58
-
-
Save santiago-salas-v/c7a2f29733dc2289c0086779f92ed46a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import sys | |
from functools import partial | |
import matplotlib | |
matplotlib.use('Qt4Agg') | |
matplotlib.rcParams['backend.qt4'] = 'PySide' | |
import matplotlib.pyplot as plt | |
from matplotlib.pyplot import figure | |
from PySide import QtGui | |
# noinspection PyArgumentList | |
def main(): | |
app = QtGui.QApplication.instance() # checks if QApplication already exists | |
if not app: # create QApplication if it doesnt exist | |
app = QtGui.QApplication(sys.argv) | |
main_form = QtGui.QWidget() | |
open_button = QtGui.QPushButton(parent=main_form) | |
open_button.clicked.connect(partial(generate_latex)) | |
open_button.setText('LaTex to png') | |
open_button.setIcon(QtGui.QIcon('glyphicons-145-folder-open.png')) | |
main_form.show() | |
main_form.adjustSize() | |
sys.exit(app.exec_()) | |
def generate_latex(): | |
file_name, ext = QtGui.QFileDialog.getOpenFileName( | |
parent=None, | |
caption='open', directory='.', | |
filter='Text description of LaTeX (*.txt)') | |
if os.path.exists(file_name): | |
with open(file_name) as f: | |
data = f.read() | |
tex_to_add = r"$" + data + r"$" | |
fig = figure(figsize=(1, 1)) | |
ax = fig.add_axes([0, 0, 1, 1], frameon=False) | |
ax.set_axis_off() | |
# add text | |
plt.text(0, 0, tex_to_add, fontsize=60) | |
# save | |
plt.savefig( | |
os.path.splitext( | |
os.path.basename(file_name))[0] + | |
'.png', | |
bbox_inches='tight', | |
transparent=True, | |
frameon=False, | |
edgecolor=None) | |
# show | |
plt.show() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment