Added some leeway to image comparison

because it turns out OpenSCAD PNG generation is non-deterministic!
This commit is contained in:
Chris Palmer 2019-06-10 10:00:47 +01:00
parent 0976e8098f
commit e4fae0ba5f
2 changed files with 23 additions and 10 deletions

View File

@ -35,6 +35,8 @@ from colorama import Fore
w = 4096
h = w
threshold = 20 # Image comparison allowed number of different pixels
fuzz = 10 # Image comparison allowed percentage error in pixel value
def do_cmd(cmd, output = sys.stdout):
for arg in cmd:
@ -42,17 +44,28 @@ def do_cmd(cmd, output = sys.stdout):
print()
return subprocess.call(cmd, stdout = output, stderr = output)
def compare_images(a, b, c):
if not os.path.isfile(a):
return -1
log_name = 'magick.log'
with open(log_name, 'w') as output:
do_cmd(("magick compare -metric AE -fuzz %d%% %s %s %s" % (fuzz, a, b, c)).split(), output = output)
with open(log_name, 'r') as f:
pixels = int(f.read().strip())
os.remove(log_name)
return pixels
def update_image(tmp_name, png_name):
"""Update an image only if different, otherwise just change the mod time"""
with open(os.devnull, 'w') as null:
diff_name = png_name.replace('.png', '_diff.png')
if not os.path.isfile(png_name) or do_cmd(("magick compare -metric AE %s %s %s" % (png_name, tmp_name, diff_name)).split(), output = null):
shutil.copyfile(tmp_name, png_name)
print(Fore.GREEN + png_name + " updated" + Fore.WHITE)
else:
os.utime(png_name, None)
os.remove(diff_name)
os.remove(tmp_name)
diff_name = png_name.replace('.png', '_diff.png')
pixels = compare_images(png_name, tmp_name, diff_name)
if pixels < 0 or pixels > threshold:
shutil.copyfile(tmp_name, png_name)
print(Fore.YELLOW + png_name + " updated" + Fore.WHITE, pixels if pixels > 0 else '')
else:
os.utime(png_name, None)
os.remove(diff_name)
os.remove(tmp_name)
def depluralise(name):

View File

@ -149,10 +149,10 @@ def views(target, do_assemblies = None):
png_name = png_name.replace('_assembly', '_assembled')
changed = check_deps(mtime(png_name), dname)
changed = times.check_have_time(changed, png_name)
tmp_name = 'tmp.png'
if changed:
print(changed)
t = time.time()
tmp_name = 'tmp.png'
openscad.run("-D$pose=1", "-D$explode=%d" % explode, "--projection=p", "--imgsize=4096,4096", "--autocenter", "--viewall", "-d", dname, "-o", tmp_name, png_maker_name);
times.add_time(png_name, t)
do_cmd(["magick", tmp_name, "-trim", "-resize", "1004x1004", "-bordercolor", "#ffffe5", "-border", "10", tmp_name])