Skip to content

Instantly share code, notes, and snippets.

@utgwkk
Created September 10, 2015 11:06
Show Gist options
  • Save utgwkk/4d7d6701aa52f90ecd76 to your computer and use it in GitHub Desktop.
Save utgwkk/4d7d6701aa52f90ecd76 to your computer and use it in GitHub Desktop.
Generates the trace from the log file generated by Piet on Go.
#!/usr/bin/env python
from PIL import Image, ImageDraw
import sys
import re
sourcefilename = sys.argv[1]
if len(sys.argv) > 2:
codelsize = int(sys.argv[2])
else:
codelsize = 1
img = Image.open(sourcefilename).convert('RGB')
outimg = Image.new("RGB", (int(img.size[0] / codelsize) * 20, int(img.size[1] / codelsize) * 20))
draw = ImageDraw.Draw(outimg)
il = img.load()
for y in range(int(img.size[1] / codelsize)):
for x in range(int(img.size[0] / codelsize)):
draw.rectangle((x*20, y*20, (x+1)*20, (y+1)*20), il[x*codelsize, y*codelsize])
pat = re.compile(r'\((\d+), ?(\d+)\)')
with open("trace.txt", "rt") as f:
oldx, oldy = 0, 0
for line in f.readlines():
srh = pat.search(line)
if srh:
x, y = int(srh.group(1))*20, int(srh.group(2))*20
draw.line((oldx+10, oldy+10, x+10, y+10), fill="gray")
draw.ellipse((x-6, y-6, x+14, y+14), fill="gray")
oldx, oldy = x, y
draw.rectangle((oldx-6, oldy-6, oldx+14, oldy+14), fill="black")
del draw
outimg.save("trace.png")
#!/bin/sh
# Usage: bash generate_trace_executor.sh [sourcecode] [codelsize]
sourcefile=$1
codelsize=$2
if test -z $sourcefile ; then
exit
fi
if test -z $codelsize ; then
codelsize=1
fi
./pietongo -i $sourcefile --debug -codel $codelsize > trace.txt
python3 generatetrace.py $sourcefile $codelsize
rm trace.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment