-
Notifications
You must be signed in to change notification settings - Fork 0
/
img-to-scm.py
56 lines (51 loc) · 1.42 KB
/
img-to-scm.py
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
try:
from PIL import Image
from numpy import array
import qrcode
except ImportError:
from os import system
# install reqs and run itself inside itself -- this probably isn't the best way to do this, but it's fitting given the program
system(f"python -m pip install Pillow numpy qrcode && python {__file__}")
exit()
QR_SIZE = 23
CANVAS_SIZE = (800 // QR_SIZE) * QR_SIZE
PX = CANVAS_SIZE // QR_SIZE
SCALE = list(range(-QR_SIZE // 2, QR_SIZE // 2))
qr = qrcode.QRCode(border=1)
qr.add_data("mse.mehvix.com")
img = qr.make_image(fit=True)
img = img.resize((23, 23), Image.NEAREST)
arr = array(img).tolist()[::-1] # unmirror
moves = []
for y, r in enumerate(arr):
for x, c in enumerate(r):
if not c:
moves.append(f"(px {SCALE[x]} {SCALE[y]})")
moves_str = "\n ".join(moves)
with open("contest.scm", "w") as fout:
fout.write(
f'\
;;; Scheme Recursive Art Contest Entry\n\
;;;\n\
;;; Please do not include your name or personal info in this file.\n\
;;;\n\
;;; Title: MSE: MSE Scheme EschQR\n\
;;;\n\
;;; Description:\n\
;;; <...drawing hands create\n\
;;; a strange loop from meaningless\n\
;;; symbols, just as hands...>\n\
\n\
(define (draw)\n\
(ht)\n\
(pixelsize {PX})\n\
{moves_str}\n\
(exitonclick))\n\
\n\
(define (px x y)\n\
(pixel x y "black")\n\
)\n\
; Please leave this last line alone. You may add additional procedures above\n\
; this line.\n\
(draw)'
)