-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr2negative.py
executable file
·80 lines (64 loc) · 1.89 KB
/
qr2negative.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
from PIL import Image
from svgwrite import Drawing as SVGDrawing
filename = "qrcode-mini.png"
outname = "qrcode-out.svg"
black = (0,0,0)
pixel_width = 7
pixel_height = pixel_width
# import QR code
img = Image.open(filename)
pixels = img.load()
width = img.size[0]
height = img.size[1]
# create output file
svg = SVGDrawing(
filename = outname,
size = (str(width*pixel_width)+"px", str(height*pixel_height)+"px")
)
def cut_if_no_neighbour(x,y, nx,ny, path):
global pixels
global svg
if pixels[nx,ny] == black:
return
svg.add(
svg.path(d = path,
fill = "none",
stroke = 'red',
stroke_width = "0.1mm"
)
)
for y in range(height):
for x in range(width):
x1 = x*pixel_width
y1 = y*pixel_height
x2 = x1+pixel_width
y2 = y1+pixel_height
if pixels[x,y] == black:
cut_if_no_neighbour(x,y, x-1,y, "M {0} {1} L {2} {3}".format(x1, y1, x1, y2))
cut_if_no_neighbour(x,y, x,y+1, "M {0} {1} L {2} {3}".format(x1, y2, x2, y2))
cut_if_no_neighbour(x,y, x+1,y, "M {0} {1} L {2} {3}".format(x2, y2, x2, y1))
cut_if_no_neighbour(x,y, x,y-1, "M {0} {1} L {2} {3}".format(x2, y1, x1, y1))
print "X",
else:
# wegschneiden
for i in range(pixel_height):
svg.add(
svg.path(
d="M {0} {1} l {2} 0".format(x1, y1+i, pixel_width),
fill="none",
stroke="red",
stroke_width="0.1mm"
)
)
print " ",
print ""
svg.save()
# add line wraps, quick and dirty
f = open(outname,'r')
content = f.read()
f.close()
content = content.replace('>', '>\n')
f = open(outname, 'w')
f.write(content)
f.close()