-
Notifications
You must be signed in to change notification settings - Fork 0
/
nll_image_compare_report.py
367 lines (246 loc) · 10.3 KB
/
nll_image_compare_report.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""
FIXME(nll) comment
Copyright (C) 2019 Nicolas Lelong ([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import json
import os
import sys
assert sys.version_info >= (3, 6) # NOTE(nll) may run on lower python versions, untested
#-----------------------------------------------------------------------------
def main(argv):
args = validateCmdLineArgs(argv)
if args is None:
return
if args.folderPath2 is None:
# Compare images in the same folder
# Variants are found using '-' in image names
folderPath = args.folderPath1
imageNames = findImagesInFolder(folderPath)
variants = findImagesVariants(imageNames, '-')
encVariants = encodeVariantsConfiguration(variants)
isOk = validateImagesVariants(encVariants)
if isOk:
configurationJSstr = "window.CFG_COMPARISONS =" + json.dumps(encVariants, indent=2)
writeAssetsToFolder(args.folderPath1)
writeConfigurationJS(args.folderPath1, configurationJSstr)
else:
# Compare images between two folders
# One folder per variant
folderPath1 = args.folderPath1
folderPath2 = args.folderPath2
rootFolderPath = os.path.split(folderPath1)[0]
if rootFolderPath.lower() != os.path.split(folderPath2)[0].lower():
logError("Both folders must be in the same root folder")
input('\nPress Enter to close.')
return
folderName1 = os.path.split(folderPath1)[1]
imageNames1 = findImagesInFolder(folderPath1, recursive=True)
folderName2 = os.path.split(folderPath2)[1]
imageNames2 = findImagesInFolder(folderPath2, recursive=True)
encVariants = encodeVariantsConfiguration2folder(folderName1, imageNames1, folderName2, imageNames2)
isOk = validateImagesVariants(encVariants)
if isOk:
configurationJSstr = "window.CFG_COMPARISONS = " + json.dumps(encVariants, indent=2)
writeAssetsToFolder(rootFolderPath)
writeConfigurationJS(rootFolderPath, configurationJSstr)
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
def findImagesVariants(imageNames, separator):
logInfo("Searching for variants...")
logInfo(" * separator is '%c'" % separator)
variants = {} # NOTE(nll) will be { basename: { variant: filename, ... } }
imageNames = sorted(imageNames)
for filename in imageNames:
name, ext = os.path.splitext(filename)
split = splitlast(name, sep=separator)
if len(split) != 2:
print(" * skipping image, no variant name found in '%s'." % filename)
continue
basename, variantname = split
basename = basename.strip()
variantname = variantname.strip()
variants.setdefault(basename, {})[variantname] = filename
logInfo(" * found %d images with variants." % len(variants))
return variants
def splitlast(str, sep):
index = str.rfind(sep)
if index == -1:
return [str]
else:
return [str[:index], str[index+1:]]
#-----------------------------------------------------------------------------
def findImagesInFolder(folderPath, recursive=False):
EXTENSIONS = ['.png', '.jpg', '.jpeg']
logInfo("Scan folder for images : '%s'..." % folderPath)
imagenames = []
for curdir, dirs, files in os.walk(folderPath):
if not recursive:
del dirs[:]
filenames = [ x for x in files if os.path.isfile(os.path.join(curdir,x)) ]
filenames = [ os.path.relpath( os.path.join(curdir,x), folderPath ) for x in filenames ]
filenames = [ x.replace('\\', '/') for x in filenames ]
for filename in filenames:
name, ext = os.path.splitext(filename)
if ext.lower() not in EXTENSIONS:
continue
imagenames.append(filename)
logInfo(" found %d images." % len(imagenames))
return imagenames
#-----------------------------------------------------------------------------
def encodeVariantsConfiguration(variants):
result = []
names = list(variants.keys())
names.sort()
for name in names:
imagevariants = variants[name]
encoded = encodeCmpValue(name, imagevariants)
result.append(encoded)
return result
#-----------------------------------------------------------------------------
def encodeVariantsConfiguration2folder(folderName1, imageNames1, folderName2, imageNames2):
logInfo("Searching for variants...")
allImages = dict()
for name in imageNames1:
allImages[name] = 1 | allImages.get(name,0)
for name in imageNames2:
allImages[name] = 2 | allImages.get(name,0)
allImageNames = list( allImages.keys() )
allImageNames.sort()
result = []
for name in allImageNames:
foldersBitMask = allImages[name]
value = {}
value['title'] = os.path.splitext(name)[0]
if foldersBitMask != 3:
logInfo(" * Skipped image without variant : '%s'" % name)
continue
url = None
if foldersBitMask & 1 != 0:
url = "%s/%s" % (folderName1, name)
value['before'] = { 'label': folderName1, 'url': url }
url = None
if foldersBitMask & 2 != 0:
url = "%s/%s" % (folderName2, name)
value['after'] = { 'label': folderName2, 'url': url }
result.append(value)
logInfo(" * found %d images with variants." % len(result))
return result
#-----------------------------------------------------------------------------
def encodeCmpValue(title, variants):
keys = list(variants.keys())[:2] # FIXME(nll) maximum 2 variants
assert len(keys) == 2
keys.sort()
value = {}
value['title'] = title
value['before'] = { 'label': keys[0], 'url': variants[keys[0]] }
value['after'] = { 'label': keys[1], 'url': variants[keys[1]] }
return value
#-----------------------------------------------------------------------------
class Arguments:
def __init__(self):
self.folderPath1 = None
self.folderPath2 = None
def validateCmdLineArgs(argv):
numArgs = len(argv)
showUsageMessage = False
result = None
if numArgs == 1:
showUsageMessage = True
elif numArgs == 2:
folderPath1 = argv[1]
folderPath1 = os.path.abspath( os.path.normpath(folderPath1) )
if not os.path.isdir(folderPath1):
logError("Invalid folder path '%s'" % folderPath1)
else:
result = Arguments()
result.folderPath1 = folderPath1
elif numArgs == 3:
folderPath1 = argv[1]
folderPath1 = os.path.abspath( os.path.normpath(folderPath1) )
if not os.path.isdir(folderPath1):
logError("Invalid folder path '%s'" % folderPath1)
folderPath1 = None
folderPath2 = argv[2]
folderPath2 = os.path.abspath( os.path.normpath(folderPath2) )
if not os.path.isdir(folderPath2):
logError("Invalid folder path '%s'" % folderPath2)
folderPath2 = None
if None not in (folderPath1, folderPath2):
result = Arguments()
result.folderPath1 = folderPath1
result.folderPath2 = folderPath2
else:
logError("Too many arguments")
logError("")
showUsageMessage = True
if showUsageMessage:
logInfo(argv[0])
logInfo(__file__)
return result
#-----------------------------------------------------------------------------
def writeAssetsToFolder(dstFolderPath):
srcNames = [
'index.html',
'nll-image-compare-report-files/configuration.js',
'nll-image-compare-report-files/BeerSlider.js',
'nll-image-compare-report-files/BeerSlider.css',
]
for name in srcNames:
data = _getAssetData("assets/"+name)
dstFilePath = os.path.join(dstFolderPath, name)
dstFilePath = os.path.normpath(dstFilePath)
dstSubfolderPath = os.path.dirname(dstFilePath)
if not os.path.isdir(dstSubfolderPath):
os.makedirs(dstSubfolderPath)
with open(dstFilePath, 'wb') as f:
f.write(data)
"""
Get absolute path to resource, works for dev and for PyInstaller
"""
_RSC_BASE_PATH = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
def _getAssetData(relativeFilePath):
# NOTE(nll) I could not get pkgutil.get_data work both in dev & PyInstaller so I rely on
# https://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile
absFilePath = os.path.join(_RSC_BASE_PATH, relativeFilePath)
absFilePath = os.path.normpath(absFilePath)
with open(absFilePath, 'rb') as f:
return f.read()
def writeConfigurationJS(dstFolderPath, configurationJSstr):
dstFilePath = 'nll-image-compare-report-files/configuration.js'
dstFilePath = os.path.join(dstFolderPath, dstFilePath)
dstFilePath = os.path.normpath(dstFilePath)
dstSubfolderPath = os.path.dirname(dstFilePath)
if not os.path.isdir(dstSubfolderPath):
os.makedirs(dstSubfolderPath)
with open(dstFilePath, 'w') as f:
f.write(configurationJSstr)
#-----------------------------------------------------------------------------
def validateImagesVariants(images):
logInfo("Images variants are :")
for image in images:
title = image['title']
variantNames = [ image['before']['label'], image['after']['label'] ]
logInfo("* '%s' :" % title)
logInfo(" * %s" % variantNames)
logInfo("")
continueStr = input("Continue ? enter yes to continue : ")
return (continueStr.strip().lower() in ('yes', 'y'))
#-----------------------------------------------------------------------------
def logError(msg):
print("*ERROR*", msg)
def logInfo(msg):
print(msg)
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
if __name__ == "__main__":
main(sys.argv)