|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
output_dir.mkdir(exist_ok=True) |
|
|
|
|
|
|
|
|
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}/") |
|
|
|
|
|
|
|
|
num_pages = (total_images + images_per_page - 1) // images_per_page |
|
|
|
|
|
for page_num in range(num_pages): |
|
|
|
|
|
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] |
|
|
|
|
|
|
|
|
fig, axes = plt.subplots(grid_size[0], grid_size[1], figsize=(30, 30), dpi=100) |
|
|
|
|
|
|
|
|
axes_flat = axes.flatten() |
|
|
|
|
|
|
|
|
for idx, (ax, img_path) in enumerate(zip(axes_flat, page_images)): |
|
|
try: |
|
|
img = mpimg.imread(img_path) |
|
|
ax.imshow(img) |
|
|
|
|
|
filename = img_path.name |
|
|
|
|
|
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') |
|
|
|
|
|
|
|
|
for idx in range(len(page_images), images_per_page): |
|
|
axes_flat[idx].axis('off') |
|
|
|
|
|
|
|
|
fig.suptitle(f'Page {page_num + 1}/{num_pages} (Images {start_idx + 1}-{end_idx}/{total_images})', |
|
|
fontsize=20, fontweight='bold') |
|
|
|
|
|
|
|
|
plt.tight_layout(rect=[0, 0, 1, 0.98]) |
|
|
|
|
|
|
|
|
output_filename = output_dir / f'grid_page_{page_num + 1:03d}.png' |
|
|
plt.savefig(output_filename, dpi=100, bbox_inches='tight') |
|
|
plt.close(fig) |
|
|
|
|
|
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__": |
|
|
|
|
|
image_directory = "min_max_segments" |
|
|
|
|
|
|
|
|
save_images_grid(image_directory, output_dir="grid_outputs", images_per_page=100, grid_size=(10, 10)) |