Skip to content

Instantly share code, notes, and snippets.

@pawlos
Created December 15, 2019 16:56
Show Gist options
  • Select an option

  • Save pawlos/c36dd82f6e535ad92522e2f83d6327cd to your computer and use it in GitHub Desktop.

Select an option

Save pawlos/c36dd82f6e535ad92522e2f83d6327cd to your computer and use it in GitHub Desktop.
Drawing the flag for Hitcon's 2019 qualifications EV3 arm challenge
from PIL import Image, ImageDraw
import math
lines = open('movements.txt').readlines()
drawing = False
(pen_x,pen_y) = (80,150)
(base_x,base_y) = (200, 150)
cnt = 0
import re
def move(how_much, drawing):
global pen_x, pen_y, base_x, base_y
if drawing:
draw.line((pen_x, pen_y, pen_x + how_much, pen_y), fill=0)
pen_x += how_much
def line(degrees, drawing):
# draw a line
global pen_x, pen_y#, base_x, base_y
new_y = pen_y + degrees
if drawing:
draw.line((pen_x, pen_y, pen_x, new_y), fill=0)
pen_y = new_y
print('new pen_y {}'.format(pen_y))
mul = 15
img = Image.new('RGB', (2000, 300), (255,255,255,255))
draw = ImageDraw.Draw(img)
for l in lines[1:]:
if 'port_motor: B' in l:
if 'speed: -15' in l:
drawing = True
elif 'speed: 15' in l:
drawing = False
if 'port_motor: C' in l:
m = re.search('rotations:\s([0-9\.]+)', l)
px = float(m.groups(1)[0])*mul
#print (px)
if 'speed: -' in l:
px = -px
move(px, drawing)
if 'port_motor: A' in l:
m = re.search('rotations:\s([\-0-9]+)', l)
px = int(m.groups(1)[0])/360.0*mul
if 'speed: -' not in l:
px = -px
line(px, drawing)
cnt += 1
img.save('flag.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment