redimentary ability to preview scenes

This commit is contained in:
Grant Sanderson
2015-06-09 11:26:12 -07:00
parent b5fbf7edea
commit b527568f5c
6 changed files with 98 additions and 36 deletions

View File

@ -3,51 +3,55 @@ import itertools as it
from PIL import Image
from constants import *
#TODO, Cleanup and refactor this file.
def tex_to_image(expression,
size = "\HUGE",
template_tex_file = TEMPLATE_TEX_FILE):
"""
Returns list of images for correpsonding with a list of expressions
"""
return_list = False
arg = expression
if not isinstance(expression, str):
#TODO, verify that expression is iterable of strings
expression = "\n".join([
"\onslide<%d>{"%count + exp + "}"
for count, exp in zip(it.count(1), expression)
])
return_list = True
filestem = os.path.join(
TEX_DIR, str(hash(expression + size))
)
if not os.path.exists(filestem + ".dvi"):
return_list = isinstance(expression, list)
simple_tex = "".join(expression)
if return_list:
expression = tex_expression_list_as_string(expression)
exp_hash = str(hash(expression + size))
image_dir = os.path.join(TEX_IMAGE_DIR, exp_hash)
if os.path.exists(image_dir):
result = [
Image.open(os.path.join(image_dir, png_file)).convert('RGB')
for png_file in os.listdir(image_dir)
]
else:
filestem = os.path.join(TEX_DIR, exp_hash)
if not os.path.exists(filestem + ".tex"):
print " ".join([
"Writing ",
"".join(arg),
"at size %s to "%(size),
filestem,
])
print "Writing %s at size %s to %s.tex"%(
simple_tex, size, filestem
)
with open(template_tex_file, "r") as infile:
body = infile.read()
body = body.replace(SIZE_TO_REPLACE, size)
body = body.replace(TEX_TEXT_TO_REPLACE, expression)
with open(filestem + ".tex", "w") as outfile:
outfile.write(body)
commands = [
"latex",
"-interaction=batchmode",
"-output-directory=" + TEX_DIR,
filestem + ".tex"
]
#TODO, Error check
os.system(" ".join(commands))
assert os.path.exists(filestem + ".dvi")
result = dvi_to_png(filestem + ".dvi")
if not os.path.exists(filestem + ".dvi"):
commands = [
"latex",
"-interaction=batchmode",
"-output-directory=" + TEX_DIR,
filestem + ".tex",
"> /dev/null"
]
#TODO, Error check
os.system(" ".join(commands))
result = dvi_to_png(filestem + ".dvi")
return result if return_list else result[0]
def tex_expression_list_as_string(expression):
return "\n".join([
"\onslide<%d>{"%count + exp + "}"
for count, exp in zip(it.count(1), expression)
])
def dvi_to_png(filename, regen_if_exists = False):
"""