diff --git a/Utilities/Python/scripts/make_smv_images.py b/Utilities/Python/scripts/make_smv_images.py index 34aa04e32d..0306408ecb 100644 --- a/Utilities/Python/scripts/make_smv_images.py +++ b/Utilities/Python/scripts/make_smv_images.py @@ -14,26 +14,30 @@ from PIL import Image import numpy as np -def compare_images(image1_path, image2_path): - - img1 = Image.open(image1_path).convert('RGB') - img2 = Image.open(image2_path).convert('RGB') +def compare_images(image1_path, image2_path, threshold=10): + img1 = Image.open(image1_path).convert("RGB") + img2 = Image.open(image2_path).convert("RGB") if img1.size != img2.size: - print(f"Warning: Images have different sizes. Resizing {image2_path} to match {image1_path}") - img2 = img2.resize(img1.size, Image.LANCZOS) + raise ValueError("Images must have the same dimensions") - arr1 = np.array(img1, dtype=np.float64) - arr2 = np.array(img2, dtype=np.float64) + arr1 = np.array(img1, dtype=np.int16) + arr2 = np.array(img2, dtype=np.int16) - # Calculate Mean Squared Error (MSE) - mse = np.mean((arr1 - arr2) ** 2) + diff = np.abs(arr1 - arr2) + pixel_diff = np.max(diff, axis=2) + + changed_pixels = np.sum(pixel_diff > threshold) + total_pixels = pixel_diff.size - # Calculate normalized similarity percentage (0-100%) - max_mse = 255.0 ** 2 - similarity_percentage = (1 - (mse / max_mse)) * 100 + difference_percentage = (changed_pixels / total_pixels) * 100 + + mse = np.mean((arr1 - arr2) ** 2) - return { 'mse': mse, 'similarity_percentage': similarity_percentage } + return { + "mse": mse, + "difference_percentage": difference_percentage + } parser = argparse.ArgumentParser(description="A script to generate Smokeview images") @@ -72,9 +76,9 @@ def compare_images(image1_path, image2_path): try: if os_name == "Linux": subprocess.run(['xvfb-run','-w','2','-s','-fp /usr/share/X11/fonts/misc -screen 0 1280x1024x24','-a',smokeview_path, - '-bindir',bindir,'-runscript', case[i] ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + '-bindir',bindir,'-runscript', '-render_overwrite', case[i] ], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) else: - subprocess.run([smokeview_path,'-bindir',bindir,'-runscript',case[i]], + subprocess.run([smokeview_path,'-bindir',bindir,'-runscript','-render_overwrite', case[i]], check=True, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) except subprocess.CalledProcessError as e: print(f"Error: Smokeview failed with return code {e.returncode}") @@ -100,8 +104,8 @@ def compare_images(image1_path, image2_path): try: results = compare_images(directory+png_file.name, refdir+png_file.name) - if results['similarity_percentage'] < 98: - print('Warning: ',png_file.name,f"{results['similarity_percentage']:.2f}%") + if results['difference_percentage'] > 1: + print('Warning: ',png_file.name,' changed ',f"{results['difference_percentage']:.2f}%") except FileNotFoundError as e: print(f"Error: Could not find image file - {e}") sys.exit(1)