33"""gen_pattern.py
44Usage example:
55python 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-
76-o, --output - output file (default out.svg)
87-r, --rows - pattern rows (default 11)
98-c, --columns - pattern columns (default 8)
1312-u, --units - mm, inches, px, m (default mm)
1413-w, --page_width - page width in units (default 216)
1514-h, --page_height - page height in units (default 279)
15+ -a, --page_size - page size (default A4), supercedes -h -w arguments
1616-H, --help - show help
1717"""
1818
@@ -51,11 +51,13 @@ def makeACirclesPattern(self):
5151
5252 def makeCheckerboardPattern (self ):
5353 spacing = self .square_size
54- for x in range (1 ,self .cols + 1 ):
55- for y in range (1 ,self .rows + 1 ):
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 ):
5658 if x % 2 == y % 2 :
57- dot = SVG ("rect" , x = x * spacing , y = y * spacing , width = spacing , height = spacing , stroke_width = "0" , fill = "black" )
58- self .g .append (dot )
59+ square = SVG ("rect" , x = x * spacing + xspacing , y = y * spacing + yspacing , width = spacing , height = spacing , fill = "black" )
60+ self .g .append (square )
5961
6062 def save (self ):
6163 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 ))
@@ -65,9 +67,9 @@ def save(self):
6567def main ():
6668 # parse command line options, TODO use argparse for better doc
6769 try :
68- opts , args = getopt .getopt (sys .argv [1 :], "Ho:c:r:T:u:s:R:w:h:" , ["help" ,"output=" ,"columns=" ,"rows=" ,
70+ opts , args = getopt .getopt (sys .argv [1 :], "Ho:c:r:T:u:s:R:w:h:a: " , ["help" ,"output=" ,"columns=" ,"rows=" ,
6971 "type=" ,"units=" ,"square_size=" ,"radius_rate=" ,
70- "page_width=" ,"page_height=" ])
72+ "page_width=" ,"page_height=" , "page_size=" ])
7173 except getopt .error , msg :
7274 print msg
7375 print "for help use --help"
@@ -79,8 +81,11 @@ def main():
7981 units = "mm"
8082 square_size = 20.0
8183 radius_rate = 5.0
82- page_width = 216 #8.5 inches
83- page_height = 279 #11 inches
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 ]
8489 # process options
8590 for o , a in opts :
8691 if o in ("-H" , "--help" ):
@@ -104,6 +109,11 @@ def main():
104109 page_width = float (a )
105110 elif o in ("-h" , "--page_height" ):
106111 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 ]
107117 pm = PatternMaker (columns ,rows ,output ,units ,square_size ,radius_rate ,page_width ,page_height )
108118 #dict for easy lookup of pattern type
109119 mp = {"circles" :pm .makeCirclesPattern ,"acircles" :pm .makeACirclesPattern ,"checkerboard" :pm .makeCheckerboardPattern }
0 commit comments