summaryrefslogtreecommitdiffstats
path: root/util/recolordocsicons/recolordocsicons.py
blob: 70dee2699b8794bbb2b5bae68480b1389b6420a7 (plain)
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
#!/usr/bin/env python

# Copyright (C) 2022 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

import argparse
import os
import pathlib
import subprocess
import sys

from distutils import spawn

def recolorizePng(pngPath, color, magick):
    print("Recolorizing: {}".format(pngPath))
    try:
        subprocess.check_call([magick,
                               pngPath,
                               "-depth", "24",
                               "-colorspace", "rgb",
                               "(", "+clone", "-background", "white", "-flatten", ")",
                               "(", "+clone", "-negate", "-alpha", "copy", ")",
                               "(", "+clone", "-fuzz", "100%", "-fill", color, "-opaque", color, ")",
                               "(", "-clone", "-1,-2", "-compose", "dst_in", "-composite", ")",
                               "-delete", "0--2",
                               pngPath])
    except subprocess.CalledProcessError:
        sys.exit("Failed to recolorize {}.".format(pngPath))


def optimizePng(pngPath, optipng):
    print("Optimizing: {}".format(pngPath))
    try:
        subprocess.check_call([optipng,
                               "-o7",
                               "-strip", "all",
                               pngPath],
                              stderr=subprocess.DEVNULL)
    except subprocess.CalledProcessError:
        sys.exit("Failed to optimize {}.".format(pngPath))


def processDocsPath(docsPath, color, magick, optipng):
    pngPaths = docsPath.glob('**/*.png')
    for pngPath in pngPaths:
        recolorizePng(pngPath, color, magick)
        optimizePng(pngPath, optipng)


def main():
    parser = argparse.ArgumentParser(description='Recolor icon .png files in the '
                                     'Documentation. '
                                     'Requires ImageMagick and Optipng in Path.')
    parser.add_argument('-docspath',
                        help='The path to process (recursively).',
                        type=pathlib.Path)
    parser.add_argument('-color',
                        help='The icon foreground color.',
                        default="#808080")
    args = parser.parse_args()

    magick = spawn.find_executable("magick")
    if magick is None:
        sys.exit("ImageMagick was not found in Path.")

    optipng = spawn.find_executable("optipng")
    if optipng is None:
        sys.exit("Optipng was not found in Path.")

    processDocsPath(args.docspath, args.color, magick, optipng)

    return 0


if __name__ == '__main__':
    sys.exit(main())