Debian Codegraph @

util/recolordocsicons/recolordocsicons.py

raw
1 #!/usr/bin/env python
2
3 # Copyright (C) 2022 The Qt Company Ltd.
4 # SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
5
6 import argparse
7 import os
8 import pathlib
9 import subprocess
10 import sys
11
12 from distutils import spawn
13
14 def recolorizePng(pngPath, color, magick):
15 print("Recolorizing: {}".format(pngPath))
16 try:
17 subprocess.check_call([magick,
18 pngPath,
19 "-depth", "24",
20 "-colorspace", "rgb",
21 "(", "+clone", "-background", "white", "-flatten", ")",
22 "(", "+clone", "-negate", "-alpha", "copy", ")",
23 "(", "+clone", "-fuzz", "100%", "-fill", color, "-opaque", color, ")",
24 "(", "-clone", "-1,-2", "-compose", "dst_in", "-composite", ")",
25 "-delete", "0--2",
26 pngPath])
27 except subprocess.CalledProcessError:
28 sys.exit("Failed to recolorize {}.".format(pngPath))
29
30
31 def optimizePng(pngPath, optipng):
32 print("Optimizing: {}".format(pngPath))
33 try:
34 subprocess.check_call([optipng,
35 "-o7",
36 "-strip", "all",
37 pngPath],
38 stderr=subprocess.DEVNULL)
39 except subprocess.CalledProcessError:
40 sys.exit("Failed to optimize {}.".format(pngPath))
41
42
43 def processDocsPath(docsPath, color, magick, optipng):
44 pngPaths = docsPath.glob('**/*.png')
45 for pngPath in pngPaths:
46 recolorizePng(pngPath, color, magick)
47 optimizePng(pngPath, optipng)
48
49
50 def main():
51 parser = argparse.ArgumentParser(description='Recolor icon .png files in the '
52 'Documentation. '
53 'Requires ImageMagick and Optipng in Path.')
54 parser.add_argument('-docspath',
55 help='The path to process (recursively).',
56 type=pathlib.Path)
57 parser.add_argument('-color',
58 help='The icon foreground color.',
59 default="#808080")
60 args = parser.parse_args()
61
62 magick = spawn.find_executable("magick")
63 if magick is None:
64 sys.exit("ImageMagick was not found in Path.")
65
66 optipng = spawn.find_executable("optipng")
67 if optipng is None:
68 sys.exit("Optipng was not found in Path.")
69
70 processDocsPath(args.docspath, args.color, magick, optipng)
71
72 return 0
73
74
75 if __name__ == '__main__':
76 sys.exit(main())