3b1b-manim/tex_utils.py

116 lines
3.6 KiB
Python
Raw Normal View History

2015-03-26 22:49:22 -06:00
import os
import itertools as it
from PIL import Image
from constants import *
2015-06-09 11:26:12 -07:00
#TODO, Cleanup and refactor this file.
2015-04-30 15:26:56 -07:00
def tex_to_image(expression,
size = "\HUGE",
template_tex_file = TEMPLATE_TEX_FILE):
2015-03-26 22:49:22 -06:00
"""
Returns list of images for correpsonding with a list of expressions
"""
2015-06-09 11:26:12 -07:00
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)
2015-03-26 22:49:22 -06:00
if not os.path.exists(filestem + ".tex"):
2015-06-09 11:26:12 -07:00
print "Writing %s at size %s to %s.tex"%(
simple_tex, size, filestem
)
2015-04-30 15:26:56 -07:00
with open(template_tex_file, "r") as infile:
2015-03-26 22:49:22 -06:00
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)
2015-06-09 11:26:12 -07:00
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")
2015-03-26 22:49:22 -06:00
return result if return_list else result[0]
2015-06-09 11:26:12 -07:00
def tex_expression_list_as_string(expression):
return "\n".join([
"\onslide<%d>{"%count + exp + "}"
for count, exp in zip(it.count(1), expression)
])
2015-03-26 22:49:22 -06:00
def dvi_to_png(filename, regen_if_exists = False):
"""
Converts a dvi, which potentially has multiple slides, into a
directory full of enumerated pngs corresponding with these slides.
Returns a list of PIL Image objects for these images sorted as they
where in the dvi
"""
possible_paths = [
filename,
os.path.join(TEX_DIR, filename),
os.path.join(TEX_DIR, filename + ".dvi"),
]
for path in possible_paths:
if os.path.exists(path):
directory, filename = os.path.split(path)
name = filename.split(".")[0]
images_dir = os.path.join(TEX_IMAGE_DIR, name)
if not os.path.exists(images_dir):
os.mkdir(images_dir)
if os.listdir(images_dir) == [] or regen_if_exists:
commands = [
"convert",
"-density",
str(PDF_DENSITY),
path,
"-size",
str(DEFAULT_WIDTH) + "x" + str(DEFAULT_HEIGHT),
os.path.join(images_dir, name + ".png")
]
os.system(" ".join(commands))
image_paths = [
os.path.join(images_dir, name)
for name in os.listdir(images_dir)
if name.endswith(".png")
]
image_paths.sort(cmp_enumerated_files)
return [Image.open(path).convert('RGB') for path in image_paths]
raise IOError("File not Found")
def cmp_enumerated_files(name1, name2):
num1, num2 = [
int(name.split(".")[0].split("-")[-1])
for name in (name1, name2)
]
return num1 - num2