forked from dzuk-mutant/orxporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_proc.py
152 lines (109 loc) · 4.86 KB
/
image_proc.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os
import pathlib
import subprocess
def render_svg(svg_in, png_out, renderer, size):
"""
Export a single SVG to a PNG based on the user's renderer choice.
"""
if renderer == 'inkscape':
cmd = ['inkscape', os.path.abspath(svg_in),
'--export-png=' + os.path.abspath(png_out),
'-h', str(size), '-w', str(size)]
elif renderer == 'rendersvg':
cmd = ['rendersvg', '-w', str(size), '-h', str(size),
os.path.abspath(svg_in), os.path.abspath(png_out)]
elif renderer == 'imagemagick':
cmd = ['convert', '-background', 'none', '-density', str(size / 32 * 128),
'-resize', str(size) + 'x' + str(size), os.path.abspath(svg_in), os.path.abspath(png_out)]
else:
raise AssertionError
try:
r = subprocess.run(cmd, stdout=subprocess.DEVNULL).returncode
except Exception as e:
raise Exception('Rasteriser invocation failed: ' + str(e))
if r:
raise Exception('Rasteriser returned error code: ' + str(r))
def convert_webp(png_in, webp_out):
"""
Converts a PNG at `png_in` to a Lossless WebP at `webp_out`.
Will raise an exception if trying to invoke the converter failed.
"""
cmd = ['cwebp', '-lossless', '-quiet', os.path.abspath(png_in), '-o', os.path.abspath(webp_out)]
try:
r = subprocess.run(cmd, stdout=subprocess.DEVNULL).returncode
except Exception as e:
raise Exception('Invoking the WebP converter (cwebp) failed: ' + str(e))
if r:
raise Exception('The WebP converter returned the following: ' + str(r))
def convert_avif(png_in, avif_out):
"""
Converts a single PNG at `png_in` to a Lossless AVIF at `avif_out`.
Will raise an exception if trying to invoke the converter failed.
"""
cmd = ['avif', '-e', os.path.abspath(png_in), '-o', os.path.abspath(avif_out), '--lossless']
try:
r = subprocess.run(cmd, stdout=subprocess.DEVNULL).returncode
except Exception as e:
raise Exception('Invoking the AVIF converter (avif) failed: ' + str(e))
if r:
raise Exception('The AVIF converter returned the following: ' + str(r))
def convert_flif(png_in, flif_out):
"""
Converts a single PNG at `png_in` to a FLIF at `flif_out`.
Will raise an exception if trying to invoke the converter failed.
"""
cmd = ['flif', '-e', '--overwrite', '-Q100', os.path.abspath(png_in), os.path.abspath(flif_out)]
try:
r = subprocess.run(cmd, stdout=subprocess.DEVNULL).returncode
except Exception as e:
raise Exception('Invoking the FLIF converter (flif) failed: ' + str(e))
if r:
raise Exception('The FLIF converter returned the following: ' + str(r))
def optimise_svg(svg_in, svgo_out):
"""
Optimises a single SVG at `svg_in` to `svgo_out`.
Will raise an exception if trying to invoke the optimiser failed.
"""
cmd = ['svgcleaner', os.path.abspath(svg_in), os.path.abspath(svgo_out), '--remove-metadata=no', '--quiet']
try:
r = subprocess.run(cmd, stdout=subprocess.DEVNULL).returncode
except Exception as e:
raise Exception('Invoking the SVG optimiser (svgcleaner) failed: ' + str(e))
if r:
raise Exception('The SVG optimiser returned the following: ' + str(r))
def crush_png(png_in, pngc_out):
"""
Crushes a single PNG at `png_in` to `png_out`.
Will raise an exception if trying to invoke the optimiser failed.
"""
cmd = ['oxipng', os.path.abspath(png_in), '--out', os.path.abspath(pngc_out), '--quiet']
try:
r = subprocess.run(cmd, stdout=subprocess.DEVNULL).returncode
except Exception as e:
raise Exception('Invoking the PNG crusher (oxipng) failed: ' + str(e))
if r:
raise Exception('The PNG crusher returned the following: ' + str(r))
def batch_add_exif_metadata(paths, metadata, max_batch=1000):
"""
Adds EXIF license metadata to an image file in batches.
(This is done in batches because exiftool is far more performant
this way than if it was done invidually)
'paths' is a list of paths of image files to have EXIF
metadata applied to them.
'max_batch' is how many input images you can feed in one command
because some operating systems have different restrictions.
"""
cmd = ['exiftool']
for tag, val in metadata.items():
cmd.append('-{}={}'.format(tag, val))
cmd.append('-overwrite_original')
remaining = list(paths)
while remaining:
batch, remaining = remaining[:max_batch], remaining[max_batch:]
try:
r = subprocess.run(cmd + batch,
stdout=subprocess.DEVNULL).returncode
except Exception as e:
raise Exception('Invoking the EXIF metadata embedding tool (exiftool) failed: ' + str(e))
if r:
raise Exception('exiftool returned error code: ' + str(r))