Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/doc/pattern_tools/gen_pattern.py
16337 views
1
#!/usr/bin/env python
2
3
"""gen_pattern.py
4
Usage example:
5
python gen_pattern.py -o out.svg -r 11 -c 8 -T circles -s 20.0 -R 5.0 -u mm -w 216 -h 279
6
-o, --output - output file (default out.svg)
7
-r, --rows - pattern rows (default 11)
8
-c, --columns - pattern columns (default 8)
9
-T, --type - type of pattern, circles, acircles, checkerboard (default circles)
10
-s, --square_size - size of squares in pattern (default 20.0)
11
-R, --radius_rate - circles_radius = square_size/radius_rate (default 5.0)
12
-u, --units - mm, inches, px, m (default mm)
13
-w, --page_width - page width in units (default 216)
14
-h, --page_height - page height in units (default 279)
15
-a, --page_size - page size (default A4), supercedes -h -w arguments
16
-H, --help - show help
17
"""
18
19
from svgfig import *
20
21
import sys
22
import getopt
23
24
class PatternMaker:
25
def __init__(self, cols,rows,output,units,square_size,radius_rate,page_width,page_height):
26
self.cols = cols
27
self.rows = rows
28
self.output = output
29
self.units = units
30
self.square_size = square_size
31
self.radius_rate = radius_rate
32
self.width = page_width
33
self.height = page_height
34
self.g = SVG("g") # the svg group container
35
36
def makeCirclesPattern(self):
37
spacing = self.square_size
38
r = spacing / self.radius_rate
39
for x in range(1,self.cols+1):
40
for y in range(1,self.rows+1):
41
dot = SVG("circle", cx=x * spacing, cy=y * spacing, r=r, fill="black", stroke="none")
42
self.g.append(dot)
43
44
def makeACirclesPattern(self):
45
spacing = self.square_size
46
r = spacing / self.radius_rate
47
for i in range(0,self.rows):
48
for j in range(0,self.cols):
49
dot = SVG("circle", cx= ((j*2 + i%2)*spacing) + spacing, cy=self.height - (i * spacing + spacing), r=r, fill="black", stroke="none")
50
self.g.append(dot)
51
52
def makeCheckerboardPattern(self):
53
spacing = self.square_size
54
xspacing = (self.width - self.cols * self.square_size) / 2.0
55
yspacing = (self.height - self.rows * self.square_size) / 2.0
56
for x in range(0,self.cols):
57
for y in range(0,self.rows):
58
if x%2 == y%2:
59
square = SVG("rect", x=x * spacing + xspacing, y=y * spacing + yspacing, width=spacing, height=spacing, fill="black", stroke="none")
60
self.g.append(square)
61
62
def save(self):
63
c = canvas(self.g,width="%d%s"%(self.width,self.units),height="%d%s"%(self.height,self.units),viewBox="0 0 %d %d"%(self.width,self.height))
64
c.save(self.output)
65
66
67
def main():
68
# parse command line options, TODO use argparse for better doc
69
try:
70
opts, args = getopt.getopt(sys.argv[1:], "Ho:c:r:T:u:s:R:w:h:a:", ["help","output=","columns=","rows=",
71
"type=","units=","square_size=","radius_rate=",
72
"page_width=","page_height=", "page_size="])
73
except getopt.error as msg:
74
print(msg)
75
print("for help use --help")
76
sys.exit(2)
77
output = "out.svg"
78
columns = 8
79
rows = 11
80
p_type = "circles"
81
units = "mm"
82
square_size = 20.0
83
radius_rate = 5.0
84
page_size = "A4"
85
# page size dict (ISO standard, mm) for easy lookup. format - size: [width, height]
86
page_sizes = {"A0": [840, 1188], "A1": [594, 840], "A2": [420, 594], "A3": [297, 420], "A4": [210, 297], "A5": [148, 210]}
87
page_width = page_sizes[page_size.upper()][0]
88
page_height = page_sizes[page_size.upper()][1]
89
# process options
90
for o, a in opts:
91
if o in ("-H", "--help"):
92
print(__doc__)
93
sys.exit(0)
94
elif o in ("-r", "--rows"):
95
rows = int(a)
96
elif o in ("-c", "--columns"):
97
columns = int(a)
98
elif o in ("-o", "--output"):
99
output = a
100
elif o in ("-T", "--type"):
101
p_type = a
102
elif o in ("-u", "--units"):
103
units = a
104
elif o in ("-s", "--square_size"):
105
square_size = float(a)
106
elif o in ("-R", "--radius_rate"):
107
radius_rate = float(a)
108
elif o in ("-w", "--page_width"):
109
page_width = float(a)
110
elif o in ("-h", "--page_height"):
111
page_height = float(a)
112
elif o in ("-a", "--page_size"):
113
units = "mm"
114
page_size = a.upper()
115
page_width = page_sizes[page_size][0]
116
page_height = page_sizes[page_size][1]
117
pm = PatternMaker(columns,rows,output,units,square_size,radius_rate,page_width,page_height)
118
#dict for easy lookup of pattern type
119
mp = {"circles":pm.makeCirclesPattern,"acircles":pm.makeACirclesPattern,"checkerboard":pm.makeCheckerboardPattern}
120
mp[p_type]()
121
#this should save pattern to output
122
pm.save()
123
124
if __name__ == "__main__":
125
main()
126
127