File size: 3,141 Bytes
a98fc5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from pathlib import Path
import numpy as np

def save_images_grid(image_dir, output_dir="grid_outputs", images_per_page=100, grid_size=(10, 10)):
    """
    Save images in a grid format, 100 images per page (10x10)
    """
    image_dir = Path(image_dir)
    output_dir = Path(output_dir)
    
    # Create output directory if it doesn't exist
    output_dir.mkdir(exist_ok=True)
    
    # Get all PNG files and sort them
    image_files = sorted(list(image_dir.glob('*.png')))
    
    if not image_files:
        print(f"No PNG files found in {image_dir}")
        return
    
    total_images = len(image_files)
    print(f"Found {total_images} images")
    print(f"Saving grid images to {output_dir}/")
    
    # Calculate number of pages
    num_pages = (total_images + images_per_page - 1) // images_per_page
    
    for page_num in range(num_pages):
        # Calculate start and end indices for this page
        start_idx = page_num * images_per_page
        end_idx = min(start_idx + images_per_page, total_images)
        page_images = image_files[start_idx:end_idx]
        
        # Create figure with subplots - larger figure size for better quality
        fig, axes = plt.subplots(grid_size[0], grid_size[1], figsize=(30, 30), dpi=100)
        
        # Flatten axes array for easier iteration
        axes_flat = axes.flatten()
        
        # Display each image
        for idx, (ax, img_path) in enumerate(zip(axes_flat, page_images)):
            try:
                img = mpimg.imread(img_path)
                ax.imshow(img)
                # Extract filename without path for title
                filename = img_path.name
                # Show full filename
                ax.set_title(filename, fontsize=10, pad=3)
                ax.axis('off')
            except Exception as e:
                print(f"Error loading {img_path}: {e}")
                ax.axis('off')
        
        # Hide remaining empty subplots
        for idx in range(len(page_images), images_per_page):
            axes_flat[idx].axis('off')
        
        # Set overall title
        fig.suptitle(f'Page {page_num + 1}/{num_pages} (Images {start_idx + 1}-{end_idx}/{total_images})', 
                    fontsize=20, fontweight='bold')
        
        # Adjust layout to prevent overlap
        plt.tight_layout(rect=[0, 0, 1, 0.98])
        
        # Save the figure
        output_filename = output_dir / f'grid_page_{page_num + 1:03d}.png'
        plt.savefig(output_filename, dpi=100, bbox_inches='tight')
        plt.close(fig)  # Close to free memory
        
        print(f"Saved page {page_num + 1}/{num_pages} to {output_filename}")
    
    print(f"\nAll grid images saved to {output_dir}/")
    print(f"You can view them with: ls {output_dir}/*.png")

if __name__ == "__main__":
    # Directory containing the images
    image_directory = "min_max_segments"
    
    # Save images in 10x10 grid, 100 per page
    save_images_grid(image_directory, output_dir="grid_outputs", images_per_page=100, grid_size=(10, 10))