-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathextract_svg_path
executable file
·56 lines (45 loc) · 1.57 KB
/
extract_svg_path
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
#!/usr/bin/env python3
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
from argparse import ArgumentParser, FileType
from xml.etree import ElementTree
from pygame.math import Vector2
from svg.path import parse_path
def complex_to_tuple(c):
return c.real, c.imag
def get_coordinates(path):
for e in path:
start = Vector2(complex_to_tuple(e.start))
yield start
yield Vector2(complex_to_tuple(path[-1].end))
def main(svg, resolution):
print('# Path: coding-challenge-racer/extract_svg_path')
print('import os.path')
print()
print('import pygame')
print()
print(f"name = '{svg.name}'")
print(f'resolution = {resolution}')
print("background = pygame.image.load(os.path.splitext(__file__)[0] + '.jpg')")
print('lines = [')
tree = ElementTree.parse(svg)
root = tree.getroot()
height = float(root.attrib['height'])
for child in root:
if child.tag == '{http://www.w3.org/2000/svg}path':
d = child.attrib['d']
path = parse_path(d)
for c in get_coordinates(path):
# c.y = height - c.y
c *= resolution
decimals = 13
c.x = round(c.x, decimals)
c.y = round(c.y, decimals)
print(f' {tuple(c)},')
print(']')
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('svg', type=FileType())
parser.add_argument('--resolution', type=float, default=1, help='Resolution of the path [m/pixel]')
args = parser.parse_args()
main(**vars(args))