-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvg_to_ico.py
76 lines (68 loc) · 2.65 KB
/
svg_to_ico.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
from subprocess import check_call, check_output
from argparse import Namespace
from PIL.Image import open as image_open
from PIL.ImageFile import ImageFile
from utils import path_to_all_images, get_arguments, get_sizes
def convert_svg_to_ico(path: str, str_sizes: list[str], tuple_sizes: list[tuple[int, int]]) -> None:
"""
Converts the svg from the given path to an ico.
Parameters
__________
path: [:class:`str`]
Path to the svg to convert.
str_sizes: [:class:`list[str]`]
List of the sizes to convert the svg to.
tuple_sizes: [:class:`list[tuple[int,int]]`]
List of the dimensions of the images that will form the ico.
"""
convert_svg_to_pngs(path, str_sizes)
convert_pngs_to_ico(path, str_sizes, tuple_sizes)
print(path, "converted.")
def convert_svg_to_pngs(path: str, str_sizes: list[str]) -> None:
"""
Converts the svg from the given path to a batch of pngs of different sizes.
Parameters
__________
path: [:class:`str`]
Path to the svg to convert.
str_sizes: [:class:`list[str]`]
List of the sizes to convert the svg to.
"""
for size in str_sizes:
check_call([
'inkscape',
'--export-area-page',
f'--export-filename={path[:-3]}{size}.png',
'--export-type=png',
'-w', size,
'-h', size,
path
])
def convert_pngs_to_ico(path: str, str_sizes: list[str], tuple_sizes: list[tuple[int, int]]) -> None:
"""
Converts the pngs (previously converted from a svg) from the given path to an ico.
Parameters
__________
path: [:class:`str`]
Path to the original svg to convert.
str_sizes: [:class:`list[str]`]
List of the sizes that the svg has been converted to.
tuple_sizes: [:class:`list[tuple[int,int]]`]
List of the dimensions of the images that will form the ico.
"""
paths: list[str] = [f'{path[:-3]}{size}.png' for size in str_sizes]
images: list[ImageFile] = [image_open(path) for path in paths]
images[-1].save(f'{path[:-3]}ico', sizes=tuple_sizes, append_images=images)
def process() -> None:
"""
Processes the conversion.
"""
arguments: Namespace = get_arguments()
if not arguments.force_reconversion:
already_made: set[str] = {f'{path[:-3]}svg' for path in path_to_all_images(arguments.directory, 'ico')}
str_sizes, tuple_sizes = get_sizes(arguments)
for path in path_to_all_images(arguments.directory, 'svg'):
if arguments.force_reconversion or path not in already_made:
convert_svg_to_ico(path, str_sizes, tuple_sizes)
if __name__ == '__main__':
process()