-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg_tools.py
58 lines (49 loc) · 2.03 KB
/
svg_tools.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
# svg-animation-builder - create stop-motion animated svg from images
# Copyright (C) 2016 Josa Wode
#
# This file is part of svg-animation-builder.
#
# svg-animation-builder is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# svg-animation-builder is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <http://www.gnu.org/licenses/>.
import xml.etree.ElementTree
def extract_path(filepath):
e = xml.etree.ElementTree.parse(filepath).getroot()
e = e.find('{http://www.w3.org/2000/svg}g')
e = e.find('{http://www.w3.org/2000/svg}path')
path = e.get('d')
return path
def extract_image_info(filepath, required_width, required_height):
e = xml.etree.ElementTree.parse(filepath).getroot()
width = e.get('width')
height = e.get('height')
#convert attribute to float
if width.endswith('px'):
width = width[:-2]
width = float(width)
if height.endswith('px'):
height = height[:-2]
height = float(height)
if required_width == required_height == None:
return (int(width), int(height), 1.0, 1.0)
if required_width != None and required_height != None:
wratio = float(required_width)/float(width)
hratio = float(required_height)/float(height)
return (required_width, required_height, wratio, hratio)
if required_width == None:
ratio = float(required_height)/height
else: #if required_height == None:
ratio = float(required_width)/width
width = width*ratio
height = height*ratio
width = int(round(width))
height = int(round(height))
return (width, height, ratio, ratio)