|
2 | 2 | # Make Smokeview images for the FDS Manuals. |
3 | 3 | # The script gets the smokeview executable appropriate for your OS from the smv repository |
4 | 4 | # 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. |
5 | 6 |
|
6 | 7 | import pandas as pd |
7 | 8 | import os |
8 | 9 | import subprocess |
9 | 10 | import shutil |
10 | 11 | import platform |
11 | 12 | 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 | + |
12 | 38 |
|
13 | 39 | parser = argparse.ArgumentParser(description="A script to generate Smokeview images") |
14 | 40 | parser.add_argument("message", nargs="?", default="null", help="Optional smokeview path") |
|
57 | 83 |
|
58 | 84 | os.chdir(original_dir) |
59 | 85 |
|
| 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