File size: 5,666 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
#!/usr/bin/env python3
"""
Interactive viewer for min_max_segments images
Shows 100 images per page in a 10x10 grid
Use arrow keys or buttons to navigate between pages
"""
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from pathlib import Path
import numpy as np
from matplotlib.widgets import Button
class ImageGridViewer:
def __init__(self, image_dir, images_per_page=100, grid_size=(10, 10)):
self.image_dir = Path(image_dir)
self.images_per_page = images_per_page
self.grid_size = grid_size
self.current_page = 0
# Get all PNG files and sort them
self.image_files = sorted(list(self.image_dir.glob('*.png')))
if not self.image_files:
print(f"No PNG files found in {image_dir}")
return
self.total_images = len(self.image_files)
self.num_pages = (self.total_images + images_per_page - 1) // images_per_page
print(f"Found {self.total_images} images in {self.num_pages} pages")
# Create the figure and axes
self.fig, self.axes = plt.subplots(grid_size[0], grid_size[1], figsize=(16, 16))
# Add navigation buttons
self.add_navigation_buttons()
# Display first page
self.display_page(0)
# Connect keyboard events
self.fig.canvas.mpl_connect('key_press_event', self.on_key_press)
# Maximize window
self.maximize_window()
plt.show()
def maximize_window(self):
"""Try to maximize the window"""
try:
manager = plt.get_current_fig_manager()
if hasattr(manager, 'window'):
if hasattr(manager.window, 'showMaximized'):
manager.window.showMaximized()
elif hasattr(manager, 'full_screen_toggle'):
manager.full_screen_toggle()
except:
pass
def add_navigation_buttons(self):
"""Add Previous and Next buttons"""
# Previous button
ax_prev = plt.axes([0.3, 0.01, 0.1, 0.03])
self.btn_prev = Button(ax_prev, 'Previous')
self.btn_prev.on_clicked(self.prev_page)
# Next button
ax_next = plt.axes([0.6, 0.01, 0.1, 0.03])
self.btn_next = Button(ax_next, 'Next')
self.btn_next.on_clicked(self.next_page)
# Page indicator text
ax_page = plt.axes([0.42, 0.01, 0.16, 0.03])
ax_page.axis('off')
self.page_text = ax_page.text(0.5, 0.5, '', ha='center', va='center', fontsize=12)
def display_page(self, page_num):
"""Display a specific page of images"""
if page_num < 0 or page_num >= self.num_pages:
return
self.current_page = page_num
# Calculate start and end indices for this page
start_idx = page_num * self.images_per_page
end_idx = min(start_idx + self.images_per_page, self.total_images)
page_images = self.image_files[start_idx:end_idx]
# Clear all axes
axes_flat = self.axes.flatten()
for ax in axes_flat:
ax.clear()
ax.axis('off')
# 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
ax.set_title(filename, fontsize=8, pad=2)
ax.axis('off')
except Exception as e:
print(f"Error loading {img_path}: {e}")
ax.axis('off')
# Update title
self.fig.suptitle(f'Page {page_num + 1}/{self.num_pages} (Images {start_idx + 1}-{end_idx}/{self.total_images})',
fontsize=14, fontweight='bold')
# Update page indicator
self.page_text.set_text(f'Page {page_num + 1} of {self.num_pages}')
# Update button states
self.btn_prev.set_active(page_num > 0)
self.btn_next.set_active(page_num < self.num_pages - 1)
# Redraw
self.fig.canvas.draw_idle()
def next_page(self, event=None):
"""Go to next page"""
if self.current_page < self.num_pages - 1:
self.display_page(self.current_page + 1)
def prev_page(self, event=None):
"""Go to previous page"""
if self.current_page > 0:
self.display_page(self.current_page - 1)
def on_key_press(self, event):
"""Handle keyboard navigation"""
if event.key == 'right':
self.next_page()
elif event.key == 'left':
self.prev_page()
elif event.key == 'home':
self.display_page(0)
elif event.key == 'end':
self.display_page(self.num_pages - 1)
elif event.key == 'q' or event.key == 'escape':
plt.close('all')
if __name__ == "__main__":
print("=" * 60)
print("Image Grid Viewer")
print("=" * 60)
print("Navigation:")
print(" - Use 'Previous' and 'Next' buttons")
print(" - Or use arrow keys: ← (previous) → (next)")
print(" - Home key: first page")
print(" - End key: last page")
print(" - Press 'q' or ESC to quit")
print("=" * 60)
# Directory containing the images
image_directory = "min_max_segments"
# Create and show the viewer
viewer = ImageGridViewer(image_directory, images_per_page=100, grid_size=(10, 10)) |