-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIconThemeConverter.py
129 lines (111 loc) · 5.9 KB
/
IconThemeConverter.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
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
import os
import json
import cairosvg
from PIL import Image
# Helper function to calculate the position of overlays and icons inside a bigger canvas
def getposition(sizea, sizeb, gravity="center"):
if gravity.lower()=="center":
resxy = (((sizea - sizeb)//2),((sizea - sizeb)//2))
elif gravity.lower()=="north":
resxy = (((sizea - sizeb)//2),0)
elif gravity.lower()=="east":
resxy = ((sizea - sizeb),((sizea - sizeb)//2))
elif gravity.lower()=="south":
resxy = (((sizea - sizeb)//2),(sizea - sizeb))
elif gravity.lower()=="west":
resxy = (0,((sizea - sizeb)//2))
elif gravity.lower()=="northwest":
resxy = (0,0)
elif gravity.lower()=="northeast":
resxy = ((sizea - sizeb),0)
elif gravity.lower()=="southwest":
resxy = (0,(sizea - sizeb))
elif gravity.lower()=="southeast":
resxy = ((sizea - sizeb),(sizea - sizeb))
else:
resxy=(0,0)
return resxy
# read the settings file
with open('settings.json', 'r') as f:
settings = json.load(f)
# get the directory with the source files from the settings
icon_definition_dir = os.fsencode(settings["path_jsonfiles"])
# check if the directory exists. if yes, get a list of containing files, if not, quit with an error message
if os.path.exists(icon_definition_dir):
icon_definitions = os.listdir(icon_definition_dir)
else:
print("Error: The directory ", icon_definition_dir, " does not exist. Please check the value of 'path_jsonfiles' in settings.json!")
quit()
# get the directory with the icon theme from the settings
icon_theme_dir = os.fsencode(settings["path_icontheme"])
# quit with an error message, if the directory doesn't exist
if not os.path.exists(icon_theme_dir):
print("Error: The directory ", icon_theme_dir, " does not exist. Please check the value of 'path_icontheme' in settings.json!")
quit()
# if we have no temp directory yet create one
if os.path.exists("./temp") == False:
os.mkdir("./temp")
# process all icon definition files
for file in icon_definitions:
if os.fsdecode(file).endswith(".json"):
filename = os.path.join(icon_definition_dir, file)
print("process icon definition file:", filename)
with open(filename, 'r') as f:
curfile = json.load(f)
# get the output directory where the icons will be saved to from the icon definition file, and create it if it doesn't exist yet
output_path = os.path.join("./output", curfile["path_out_ico"])
print("target directory:", output_path)
if os.path.exists(output_path) == False:
os.makedirs(output_path)
# process all icons in the current icon definition file
for icon in curfile["icon"]:
images=[]
sizes=[]
# read all inputfiles for this icon
for inputfile in icon["file_in"]:
imagefile = os.path.join(settings["path_icontheme"], inputfile["src"])
if not os.path.exists(imagefile):
print("warning: unable to find source image file:", imagefile, "\n\t Skip it.")
continue
tempfile = str(inputfile["size"]) + ".png"
tempfile = os.path.join("./temp", tempfile)
cairosvg.svg2png(url=imagefile, output_width = inputfile["size"], output_height = inputfile["size"], write_to=tempfile)
tempsize=(inputfile["size"],inputfile["size"])
# if we have a defined image as overlay, paste it, related to its gravity and size
if "overlay" in inputfile:
ovlsrc = os.path.join(settings["path_icontheme"], inputfile["overlay"]["src"])
if os.path.exists(ovlsrc):
cairosvg.svg2png(url=ovlsrc, output_width = inputfile["overlay"]["size"], output_height = inputfile["overlay"]["size"], write_to="./temp/overlay.png")
icn = Image.open(tempfile)
icn.paste(Image.open("./temp/overlay.png"),getposition(inputfile["size"],inputfile["overlay"]["size"], inputfile["overlay"]["gravity"]),Image.open("./temp/overlay.png"))
icn.save(tempfile)
else:
print("warning: unable to find overlay image file:", ovlsrc, "\n\t Skip it and ignore the overlay.")
# if we have a specific canvas size, create a new image of its size and there paste the icon related to the gravity
if "canvas" in inputfile:
can = Image.new(mode="RGBA",size=(inputfile["canvas"]["size"],inputfile["canvas"]["size"]))
can.paste(Image.open(tempfile),getposition(inputfile["canvas"]["size"],inputfile["size"], inputfile["canvas"]["gravity"]))
can.save(tempfile)
tempsize=(inputfile["canvas"]["size"],inputfile["canvas"]["size"])
# if there are different colordepths specified, convert to them
if "colordepth" in inputfile:
for color in inputfile["colordepth"]:
if color == 32:
images.append(Image.open(tempfile))
sizes.append(tempsize)
else:
print(color)
#im = Image.new(mode="P", size=tempsize)
#images.append(im)
#sizes.append(tempsize)
else:
images.append(Image.open(tempfile))
sizes.append(tempsize)
# get the target filename and save the images into an .ico file
output_file = os.path.join(output_path, icon["file_out"])
print("save icon:", output_file)
im = Image.open(tempfile)
im.save(output_file, format="ICO", sizes = sizes, append_images = images, bitmap_format="bmp")
continue
else:
continue