forked from kovacsv/VisualScriptEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateIcons.py
More file actions
147 lines (133 loc) · 4.59 KB
/
Copy pathGenerateIcons.py
File metadata and controls
147 lines (133 loc) · 4.59 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import sys
import re
import subprocess
import codecs
from PIL import Image
def GetFileContent (fileName):
file = codecs.open (fileName, 'r', 'utf-8')
content = file.read ()
file.close ()
return content
def SetFileContent (fileName, content):
file = codecs.open (fileName, 'w', 'utf-8')
file.write (content)
file.close ()
def GenerateColoredSvg (sourceIconPath, coloredSvgIconPath, color):
sourceContent = GetFileContent (sourceIconPath)
coloredSvgContent = sourceContent
coloredSvgContent = re.sub (r'#[0-9a-fA-F]{6}', color, coloredSvgContent)
SetFileContent (coloredSvgIconPath, coloredSvgContent)
def GeneratePng (inkscapePath, sourceSvgPath, resultPngPath, size):
command = [
inkscapePath,
'--export-png=' + resultPngPath,
'--export-width=' + str (size),
'--export-height=' + str (size),
sourceSvgPath
]
subprocess.call (command)
def GenerateColoredSvgsFromSvgs (sourceFolder, targetFolder, color):
if not os.path.exists (targetFolder):
os.makedirs (targetFolder)
for svgName in os.listdir (sourceFolder):
sourceIconPath = os.path.join (sourceFolder, svgName)
coloredSvgIconPath = os.path.join (targetFolder, svgName)
GenerateColoredSvg (sourceIconPath, coloredSvgIconPath, color)
def GeneratePngsFromSvgs (inkscapePath, iconsPath, svgFolderName, pngFolderPrefix, sizes):
svgIconsPath = os.path.join (iconsPath, svgFolderName)
for svgName in os.listdir (svgIconsPath):
for size in sizes:
pngIconsPath = os.path.join (iconsPath, pngFolderPrefix + str (size))
if not os.path.exists (pngIconsPath):
os.makedirs (pngIconsPath)
svgBaseName = os.path.splitext (svgName)[0]
sourceIconPath = os.path.join (svgIconsPath, svgName)
targetPngPath = os.path.join (pngIconsPath, svgBaseName + '.png')
GeneratePng (inkscapePath, sourceIconPath, targetPngPath, size)
def GeneratePngsFromSvgs (inkscapePath, sourcePath, targetPath, size):
if not os.path.exists (targetPath):
os.makedirs (targetPath)
for svgName in os.listdir (sourcePath):
svgBaseName = os.path.splitext (svgName)[0]
sourceIconPath = os.path.join (sourcePath, svgName)
targetPngPath = os.path.join (targetPath, svgBaseName + '.png')
GeneratePng (inkscapePath, sourceIconPath, targetPngPath, size)
def GenerateBmpsFromPngs (sourcePath, targetPath):
if not os.path.exists (targetPath):
os.makedirs (targetPath)
for pngName in os.listdir (sourcePath):
pngBaseName = os.path.splitext (pngName)[0]
sourcePngPath = os.path.join (sourcePath, pngName)
targetBmpPath = os.path.join (targetPath, pngBaseName + '.bmp')
image = Image.open (sourcePngPath)
image.save (targetBmpPath)
def Main (argv):
currentDir = os.path.dirname (os.path.abspath (__file__))
os.chdir (currentDir)
if len (argv) != 2:
print ('usage: GenerateIcons.py <inkscapePath>')
print ('example: GenerateIcons.py "C:\Program Files\Inkscape\inkscape.com"')
return 1
inkscapePath = sys.argv[1]
iconDescriptors = [
{
'path' : os.path.abspath (os.path.join ('..', 'Documentation', 'CommandIcons')),
'settings' : {
'colors' : [
{ 'name' : 'gray', 'color' : '#BBBBBB' }
],
'pngs' : [
{
'color' : None,
'sizes' : [18, 36],
'bmp' : True
},
{
'color' : 'gray',
'sizes' : [18, 36],
'bmp' : True
}
]
}
},
{
'path' : os.path.abspath (os.path.join ('..', 'Documentation', 'NodeIcons')),
'settings' : {
'colors' : [
{ 'name' : 'white', 'color' : '#FAFAFA' }
],
'pngs' : [
{
'color' : None,
'sizes' : [18, 36],
'bmp' : True
},
{
'color' : 'white',
'sizes' : [18, 36],
'bmp' : True
}
]
}
}
]
for iconDescriptor in iconDescriptors:
svgIconsPath = os.path.join (iconDescriptor['path'], 'svg')
for color in iconDescriptor['settings']['colors']:
coloredSvgIconsPath = os.path.join (iconDescriptor['path'], 'svg_' + color['name'])
GenerateColoredSvgsFromSvgs (svgIconsPath, coloredSvgIconsPath, color['color'])
for png in iconDescriptor['settings']['pngs']:
svgSourcePath = os.path.join (iconDescriptor['path'], 'svg')
colorStr = ''
if png['color'] != None:
colorStr = '_' + png['color']
svgSourcePath = os.path.join (iconDescriptor['path'], 'svg' + colorStr)
for size in png['sizes']:
pngTargetPath = os.path.join (iconDescriptor['path'], 'png' + colorStr + '_' + str (size))
GeneratePngsFromSvgs (inkscapePath, svgSourcePath, pngTargetPath, size)
if png['bmp']:
bmpTargetPath = os.path.join (iconDescriptor['path'], 'bmp' + colorStr + '_' + str (size))
GenerateBmpsFromPngs (pngTargetPath, bmpTargetPath)
return 0
sys.exit (Main (sys.argv))