Skip to content

Commit 91da8b0

Browse files
committed
[markers] generate cross and support for png and pdf
1 parent c6c4590 commit 91da8b0

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

doc/sphinx/source/markers/markers.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,31 @@ Here is the usage and options:
4444
.. code::
4545
4646
usage: generate.py [-h] [--rings N] [--outdir dir] [--margin N] [--radius N]
47-
[--addId] [--whiteBackground]
47+
[--addId] [--addCross] [--generatePng] [--generatePdf]
48+
[--whiteBackground]
4849
4950
Generate the svg file for the markers.
5051
5152
optional arguments:
5253
-h, --help show this help message and exit
5354
--rings N the number of rings (possible values {3, 4}, default: 3)
54-
--outdir dir the directory where to save the files
55+
--outdir dir the directory where to save the files (default: ./)
5556
--margin N the margin to add around the external ring (default: 400)
5657
--radius N the radius of the outer circle (default: 500)
5758
--addId add the marker id on the top left corner
59+
--addCross add a small cross in the center of the marker
60+
--generatePng also generate a png file
61+
--generatePdf also generate a pdf file
5862
--whiteBackground set the background (outside the marker) to white instead
5963
of transparent
6064
65+
6166
For example, calling:
6267

6368
.. code:: shell
6469
6570
./generate.py --outdir markers3 --margin 100 --addId
6671
6772
it will create a directory :code:`markers3` where it saves an svg file for each marker with a margin around the marker of 100 and with the ID of the marker printed on the top left corner.
73+
74+
To generate pdf and/or png file, use the flags :code:`--generatePdf` and :code:`--generatePng` .

markersToPrint/generators/generate.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
import svgwrite
77

8+
from svglib.svglib import svg2rlg
9+
from reportlab.graphics import renderPDF, renderPM
10+
811
if __name__ == "__main__":
912

1013
parser = argparse.ArgumentParser(description='Generate the svg file for the markers.')
@@ -18,6 +21,12 @@
1821
help='the radius of the outer circle (default: %(default)s)')
1922
parser.add_argument('--addId', action='store_true',
2023
help='add the marker id on the top left corner')
24+
parser.add_argument('--addCross', action='store_true',
25+
help='add a small cross in the center of the marker')
26+
parser.add_argument('--generatePng', action='store_true',
27+
help='also generate a png file')
28+
parser.add_argument('--generatePdf', action='store_true',
29+
help='also generate a pdf file')
2130
parser.add_argument('--whiteBackground', action='store_true',
2231
help='set the background (outside the marker) to white instead of transparent')
2332

@@ -28,17 +37,17 @@
2837
scale = args.radius / 100
2938

3039
# size of the image, diameter + margin
31-
width = size+args.margin
32-
height = size+args.margin
40+
width = size + args.margin
41+
height = size + args.margin
3342

3443
# id of the first marker
3544
markerId = 1
3645

3746
# font size for the id
38-
font_size = int(0.037*height)
47+
font_size = int(0.037 * height)
3948

4049
# center of the marker
41-
center = (width/2, height/2)
50+
center = (width / 2, height / 2)
4251

4352
radius = args.radius
4453

@@ -56,7 +65,8 @@
5665
for line in f:
5766

5867
# name of the output file
59-
out_filename = os.path.join(args.outdir, str(markerId).zfill(4)+'.svg')
68+
base_filename = os.path.join(args.outdir, str(markerId).zfill(4))
69+
out_filename = base_filename + '.svg'
6070

6171
# create the svg
6272
dwg = svgwrite.Drawing(out_filename, profile='tiny', size=(width, height))
@@ -67,7 +77,7 @@
6777
dwg.add(dwg.text(text=str(markerId), insert=(5, 50), font_size=font_size))
6878

6979
# print the outer circle as black
70-
dwg.add(dwg.circle(center=center, r=size/2, fill='black'))
80+
dwg.add(dwg.circle(center=center, r=size / 2, fill='black'))
7181

7282
fill_color = 'white'
7383
count = 0
@@ -76,7 +86,7 @@
7686
for r in line.split():
7787
radius = int(r)
7888
# print(r)
79-
dwg.add(dwg.circle(center=center, r=scale*radius, fill=fill_color))
89+
dwg.add(dwg.circle(center=center, r=scale * radius, fill=fill_color))
8090
if fill_color == 'white':
8191
fill_color = 'black'
8292
else:
@@ -89,6 +99,18 @@
8999
else:
90100
assert count == 7
91101

102+
if args.addCross:
103+
# print a small cross in the center
104+
dwg.add(dwg.line(start=(center[0] - 10, center[1]), end=(center[0] + 10, center[1]), stroke="gray"))
105+
dwg.add(dwg.line(start=(center[0], center[1] - 10), end=(center[0], center[1] + 10), stroke="gray"))
106+
92107
dwg.save(pretty=True)
93108

94-
markerId = markerId + 1
109+
if args.generatePng or args.generatePdf:
110+
drawing = svg2rlg(out_filename)
111+
if args.generatePdf:
112+
renderPDF.drawToFile(drawing, base_filename + ".pdf")
113+
if args.generatePng:
114+
renderPM.drawToFile(drawing, base_filename + ".png", fmt="PNG")
115+
116+
markerId = markerId + 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
svgwrite
2+
svglib

0 commit comments

Comments
 (0)