Python Forum
Latex image too big on tkinter using sympy - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Latex image too big on tkinter using sympy (/thread-30723.html)



Latex image too big on tkinter using sympy - 4096 - Nov-03-2020

I'm trying to create small images of mathematical forumulas using tkinter, sympy, and latex. The images generated are way too big and I don't know how to reduce the size. When I searched online, people said that using \documentclass{standalone} will make the generated image the minimum size sufficient to fit the contents, but that hasn't happened when I tried it. The images generated seem to be 600 by 800 points no matter what I try. I've tried various things like adding the option [convert={size=25x50}] to the document class and \usepackage[active,tightpage,textmath]{preview} (per this page https://tex.stackexchange.com/questions/146511/using-usepackageactive-tightpagepreview-does-not-generate-any-page ) and \documentclass[tightpage]{standalone} but nothing worked.

I made a test program to experiment:

import tkinter as tk
import sympy as sp
from PIL import Image, ImageTk
from io import BytesIO
import os

def latex(s, label, fontsize=1, height=100, width=300):
    stream = BytesIO()
    sp.preview(r"$%s$\end{document}" % s, euler=False, viewer="BytesIO", output="ps",\
        outputbuffer=stream, premable=r"\documentclass[tightpage]{standalone}\begin{document}") # [convert={size=25x50}]
    image = Image.open(stream)
    image.resize((height, width))
    image.load(scale=fontsize)
    photo = ImageTk.PhotoImage(image)
    label.config(image=photo)
    label.image=photo
    stream.close()

def loadLatex():
    latex(var.get(), label, fontsize=10, height=1, width=2)

win = tk.Tk()
label = tk.Label(win)
var = tk.StringVar()
tk.Entry(win, textvariable=var).grid(row=0, column=0)
tk.Button(win, text="Latex", command=loadLatex).grid(row=1, column=0)
label.grid(row=2, column=0)
win.mainloop()



RE: Latex image too big on tkinter using sympy - DPaul - Nov-05-2020

Hi, 2 observations:

1) your default output is 600x800, that is a classic 3/4.
You seem to resize to 1/3. Maybe it does not matter, but it usually does.

2) I use a different syntax to resize some png cards:

crd = Image.open('cards/' + 'card1.png')
img = ImageTk.PhotoImage(crd.resize((167, 242)))

Hope it helps,
Paul