forked from PyLadiesCZ/roboprojekt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
export_img.py
58 lines (50 loc) · 1.4 KB
/
export_img.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
"""
Program export images in svg format to png format.
Open file in directory where you have svg images saved.
"""
import sys
import subprocess
from pathlib import Path
base = Path(".") #current directory where are svg images
inkscape_paths = [
"inkscape",
"C:/Program Files/Inkscape/inkscape",
"C:/Program Files (x86)/Inkscape/inkscape"
]
def run_inkscape(path):
"""
Check if inscape path run Inscape
"""
try:
subprocess.run([path, "--version"],
stdin=subprocess.DEVNULL,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
return True
except FileNotFoundError:
return False
def find_inkscape_path():
"""
Return first functional Inkscape path
"""
for path in inkscape_paths:
if run_inkscape(path):
return path
inkscape = find_inkscape_path()
def export_svg_png():
"""
Export img in svg to img.png
"""
for img in base.glob("*.svg"):
name = str(img)
parts = []
new_name = ""
for part in img.with_suffix(".png").parts:
if part == "svg":
parts.append("png")
else:
parts.append(part)
new_name = str(Path(*parts))
subprocess.run([inkscape, name, "--export-png="+ new_name, "--export-area-page"],check = True,)
export_svg_png()
print("Done")