Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 97 lines (85 sloc) 3.442 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
from PIL import Image
ntimes = 135
itimes = range(1, ntimes+1)
megayears = [float(i)/100.0 for i in itimes]

filemetapattern = "hsv-%%s-cut-%%s-Bstar-ep-%4.4i.png"
# Example: hsv-xtd-cut-yz-Bstar-ep-1180.png
filepatterns = [filemetapattern % (10*i) for i in itimes]

varids = ["bbb", "xtd"]
cutids = ["xy", "xz", "yz"]

nx, ny = 512, 512 # Desired size of each pane. Original is scaled up to this
mx, my = 16, 16 # Margin sizes



##
## Machinery for converting text -> image
##
import pyx, subprocess, hashlib
pyx.text.set(mode="latex")
preambletext = r"""
\usepackage{color}
\usepackage[varg]{txfonts}
%\AtBeginDocument{\pagecolor{black}}
"""
pyx.text.preamble(preambletext)
gscommand = """
gs -sDEVICE=pngalpha -sOutputFile=%s.png -r300 -dBATCH -dNOPAUSE -dSAFER -q -dNOCACHE %s.pdf
"""
tmpfilepattern = "/tmp/mhdcuts-stitchup-%s"

def text2image(text):
    """
Converts argument (interpreted as LaTeX) into an antialiased PNG image with alpha channel

Implements simple caching of images to save on LaTeX and gs processing
"""
    # Temporary filename is unique for given combo of text and other relevant stuff
    tmpprefix = tmpfilepattern % (hashlib.md5(text+preambletext+gscommand+textwrapper).hexdigest())
    try:
        # use pre-generated temporary PNG file if possible
        im = Image.open("%s.png" % (tmpprefix))
    except IOError:
        c = pyx.canvas.canvas()
        c.text(0, 0, textwrapper % (text), [pyx.trafo.scale(1.0)])
        c.writePDFfile(tmpprefix)
        subprocess.Popen(gscommand % (tmpprefix, tmpprefix), shell=True).wait()
        im = Image.open("%s.png" % (tmpprefix))
    return im

vartexts = [r"\(B\)", r"\(x\), \(T\), \(\rho\)"]
cuttexts = [r"\(xy\) plane", r"\(xz\) plane", r"\(yz\) plane"]
textwrapper = r"{\color{white}\raggedright\bfseries\boldmath %s}"
vartextimages = [text2image(vartext) for vartext in vartexts]
cuttextimages = [text2image(cuttext) for cuttext in cuttexts]
cuttext_height = max([im.size[1] for im in cuttextimages])
##
## Stitch panels together
##
keyimages = [Image.open("mhdcuts-Bkey-%s.png" % (i)) for i in ["val", "sat"]]

for filepattern, itime, megayear in zip(filepatterns, itimes, megayears):
    print filepattern
    rows = [[Image.open(filepattern % (varid, cutid)).resize((nx,ny))
             for cutid in cutids] for varid in varids]
    image = Image.new('RGB', (3*nx + 4*mx,
                              2*ny + 3*my + cuttext_height))
    oy = my
    ox = mx
    for j, row in enumerate(rows):
        ox = mx
        for pane in row:
            image.paste(pane, (ox, oy))
            # image.paste(vartextimages[j], (ox + mx, oy + my), vartextimages[j])
            ox += nx + mx
        oy += ny + my
    ox = mx
    # for cuttextimage in cuttextimages:
    # dx = (nx - cuttextimage.size[0])/2
    # image.paste(cuttextimage, (ox + dx, oy), cuttextimage)
    # ox += nx + mx
    # oy += cuttext_height + mx
    timetext = r"\(t = %.2f\) Myr" % (megayear)
    timetextimage = text2image(timetext)
    ox = mx
    image.paste(timetextimage, (ox, oy), timetextimage)

    oy = my
    # oy = ny + my + my/2 - keyimages[0].size[1]/2
    ox = mx + mx/2 + nx - keyimages[0].size[0]/2
    image.paste(keyimages[0], (ox, oy), keyimages[0])
    ox += nx + mx
    image.paste(keyimages[1], (ox, oy), keyimages[1])

    image.save("mhdcuts-Bstar-stitchup-nolabels-%3.3i.png" % (itime))
Something went wrong with that request. Please try again.