#!/usr/bin/env python3 import cv2 import numpy as np from pathlib import Path import sys def create_image_grid(image_files, grid_size=(10, 10), img_width=1000, img_height=200): """ Create a grid of images with specified dimensions """ rows, cols = grid_size images_to_show = image_files[:rows * cols] # Create a blank canvas for the grid # Add some padding between images padding = 5 canvas_width = cols * img_width + (cols - 1) * padding canvas_height = rows * img_height + (rows - 1) * padding # Create white background canvas = np.ones((canvas_height, canvas_width, 3), dtype=np.uint8) * 255 for idx, img_path in enumerate(images_to_show): row = idx // cols col = idx % cols try: # Read and resize image img = cv2.imread(str(img_path)) if img is None: print(f"Warning: Could not read {img_path}") continue # Resize to target dimensions img_resized = cv2.resize(img, (img_width, img_height)) # Calculate position on canvas y_start = row * (img_height + padding) y_end = y_start + img_height x_start = col * (img_width + padding) x_end = x_start + img_width # Place image on canvas canvas[y_start:y_end, x_start:x_end] = img_resized # Add filename text filename = img_path.name font = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.4 thickness = 1 text_color = (0, 0, 0) # Black text # Put text at the top of each image text_x = x_start + 5 text_y = y_start + 15 cv2.putText(canvas, filename, (text_x, text_y), font, font_scale, text_color, thickness) except Exception as e: print(f"Error processing {img_path}: {e}") return canvas def display_images_opencv(image_dir, images_per_page=100, grid_size=(10, 10)): """ Display images using OpenCV with specified resolution """ image_dir = Path(image_dir) # 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) num_pages = (total_images + images_per_page - 1) // images_per_page print(f"Found {total_images} images") print(f"Will display in {num_pages} pages") print("\nControls:") print(" - Press SPACE or ENTER to go to next page") print(" - Press 'b' to go back to previous page") print(" - Press 'q' or ESC to quit") print(" - Press 's' to save current page as image") print("-" * 50) current_page = 0 while True: # Calculate start and end indices for this page start_idx = current_page * images_per_page end_idx = min(start_idx + images_per_page, total_images) page_images = image_files[start_idx:end_idx] # Create grid for current page print(f"\nPreparing page {current_page + 1}/{num_pages} (Images {start_idx + 1}-{end_idx}/{total_images})...") grid_image = create_image_grid(page_images, grid_size, img_width=1000, img_height=200) # Use original size without scaling display_image = grid_image h, w = grid_image.shape[:2] print(f"Display size: {w}x{h} pixels") # Create window title window_name = f'Image Grid - Page {current_page + 1}/{num_pages}' # Display the image cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) cv2.imshow(window_name, display_image) # Wait for key press key = cv2.waitKey(0) & 0xFF # Handle key press if key == ord('q') or key == 27: # q or ESC print("\nQuitting...") break elif key == ord(' ') or key == 13: # SPACE or ENTER if current_page < num_pages - 1: current_page += 1 cv2.destroyAllWindows() else: print("Already at last page!") elif key == ord('b'): # b for back if current_page > 0: current_page -= 1 cv2.destroyAllWindows() else: print("Already at first page!") elif key == ord('s'): # s for save save_filename = f'saved_grid_page_{current_page + 1:03d}.png' cv2.imwrite(save_filename, grid_image) print(f"Saved current page to {save_filename}") elif key == ord('1'): # Jump to first page current_page = 0 cv2.destroyAllWindows() elif key == ord('0'): # Jump to last page current_page = num_pages - 1 cv2.destroyAllWindows() cv2.destroyAllWindows() print("Display complete!") if __name__ == "__main__": # Directory containing the images image_directory = "min_max_segments" # Display images with OpenCV display_images_opencv(image_directory, images_per_page=100, grid_size=(10, 10))