Python Forum
Latex image too big on tkinter using sympy
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Latex image too big on tkinter using sympy
#1
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/...e-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()
Gribouillis likes this post
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 396 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  My Background Image Is Not Appearing (Python Tkinter) HailyMary 2 3,972 Mar-14-2023, 06:13 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,300 Aug-02-2022, 09:13 PM
Last Post: woooee
  [Tkinter] Tkinter don't change the image DQT 2 1,556 Jul-22-2022, 10:26 AM
Last Post: menator01
  How to redraw an existing image to a different image TkInter zazas321 6 5,744 Jul-08-2021, 07:44 PM
Last Post: deanhystad
  tkinter showing image in button rwahdan 3 5,517 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 3,954 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  how to resize image in canvas tkinter samuelmv30 2 17,547 Feb-06-2021, 03:35 PM
Last Post: joe_momma
Photo Tkinter TEXT background image _ShevaKadu 5 7,649 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  tkinter: Image to Label Maryan 10 5,108 Oct-29-2020, 01:48 PM
Last Post: joe_momma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020