|
| 1 | +import os |
| 2 | +from PIL import Image, ImageChops |
| 3 | + |
| 4 | +def images_are_identical(img1_path, img2_path): |
| 5 | + # Open both images |
| 6 | + with Image.open(img1_path) as img1, Image.open(img2_path) as img2: |
| 7 | + # Check if dimensions are the same |
| 8 | + if img1.size != img2.size: |
| 9 | + return False |
| 10 | + |
| 11 | + # Check if pixel values are the same |
| 12 | + diff = ImageChops.difference(img1, img2) |
| 13 | + return not diff.getbbox() # getbbox() returns None if images are identical |
| 14 | + |
| 15 | +def compare_png_images(dir1, dir2): |
| 16 | + # List to hold mismatched files, if any |
| 17 | + mismatched_files = [] |
| 18 | + |
| 19 | + # Walk through both directories simultaneously |
| 20 | + for root, _, files in os.walk(dir1): |
| 21 | + for file in files: |
| 22 | + if file.endswith('.png'): |
| 23 | + # Full path for file in dir1 |
| 24 | + path1 = os.path.join(root, file) |
| 25 | + |
| 26 | + # Construct corresponding path for file in dir2 |
| 27 | + relative_path = os.path.relpath(path1, dir1) |
| 28 | + path2 = os.path.join(dir2, relative_path) |
| 29 | + |
| 30 | + # Check if the file exists in the second directory |
| 31 | + if not os.path.exists(path2): |
| 32 | + print(f"File missing in second directory: {relative_path}") |
| 33 | + mismatched_files.append(relative_path) |
| 34 | + continue |
| 35 | + |
| 36 | + # Compare images |
| 37 | + if not images_are_identical(path1, path2): |
| 38 | + print(f"Images do not match: {relative_path}") |
| 39 | + mismatched_files.append(relative_path) |
| 40 | + |
| 41 | + # Final output |
| 42 | + if not mismatched_files: |
| 43 | + print("All .png images are identical.") |
| 44 | + else: |
| 45 | + print("Some images do not match or are missing in the second directory.") |
| 46 | + |
| 47 | +# Example usage |
| 48 | +compare_png_images('/global/cfs/cdirs/e3sm/www/chengzhu/complete_run_11112024/e3sm_diags_extended', '/global/cfs/cdirs/e3sm/www/chengzhu/tutorial2024/e3sm_diags_extended') |
0 commit comments