Skip to content

Commit edbe5fb

Browse files
committed
Python: Add image comparison tool
1 parent eba88e3 commit edbe5fb

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Utilities/Python/scripts/make_smv_images.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,39 @@
22
# Make Smokeview images for the FDS Manuals.
33
# The script gets the smokeview executable appropriate for your OS from the smv repository
44
# unless you provide an optional full path to the executable as an argument.
5+
# After images are created, a comparison is made with the reference figures in the fig repository.
56

67
import pandas as pd
78
import os
89
import subprocess
910
import shutil
1011
import platform
1112
import argparse
13+
import sys
14+
from PIL import Image
15+
import numpy as np
16+
17+
def compare_images(image1_path, image2_path):
18+
19+
img1 = Image.open(image1_path).convert('RGB')
20+
img2 = Image.open(image2_path).convert('RGB')
21+
22+
if img1.size != img2.size:
23+
print(f"Warning: Images have different sizes. Resizing {image2_path} to match {image1_path}")
24+
img2 = img2.resize(img1.size, Image.LANCZOS)
25+
26+
arr1 = np.array(img1, dtype=np.float64)
27+
arr2 = np.array(img2, dtype=np.float64)
28+
29+
# Calculate Mean Squared Error (MSE)
30+
mse = np.mean((arr1 - arr2) ** 2)
31+
32+
# Calculate normalized similarity percentage (0-100%)
33+
max_mse = 255.0 ** 2
34+
similarity_percentage = (1 - (mse / max_mse)) * 100
35+
36+
return { 'mse': mse, 'similarity_percentage': similarity_percentage }
37+
1238

1339
parser = argparse.ArgumentParser(description="A script to generate Smokeview images")
1440
parser.add_argument("message", nargs="?", default="null", help="Optional smokeview path")
@@ -57,3 +83,29 @@
5783

5884
os.chdir(original_dir)
5985

86+
# Compare images just created against the reference images in the fig repository
87+
88+
from pathlib import Path
89+
90+
mandir = '../../Manuals/'
91+
refdir = '../../../fig/fds/Reference_Figures/'
92+
directories = [mandir+'FDS_User_Guide/SCRIPT_FIGURES/' , mandir+'FDS_Verification_Guide/SCRIPT_FIGURES/']
93+
94+
print('comparing images with reference figures...')
95+
96+
for directory in directories:
97+
dir_path = Path(directory)
98+
99+
for png_file in dir_path.glob("*.png"):
100+
101+
try:
102+
results = compare_images(directory+png_file.name, refdir+png_file.name)
103+
if results['similarity_percentage'] < 90:
104+
print('Warning: ',png_file.name,f"{results['similarity_percentage']:.2f}%")
105+
except FileNotFoundError as e:
106+
print(f"Error: Could not find image file - {e}")
107+
sys.exit(1)
108+
except Exception as e:
109+
print(f"Error: {e}")
110+
sys.exit(1)
111+

0 commit comments

Comments
 (0)