| |
| 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] |
| |
| |
| |
| padding = 5 |
| canvas_width = cols * img_width + (cols - 1) * padding |
| canvas_height = rows * img_height + (rows - 1) * padding |
| |
| |
| 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: |
| |
| img = cv2.imread(str(img_path)) |
| if img is None: |
| print(f"Warning: Could not read {img_path}") |
| continue |
| |
| |
| img_resized = cv2.resize(img, (img_width, img_height)) |
| |
| |
| y_start = row * (img_height + padding) |
| y_end = y_start + img_height |
| x_start = col * (img_width + padding) |
| x_end = x_start + img_width |
| |
| |
| canvas[y_start:y_end, x_start:x_end] = img_resized |
| |
| |
| filename = img_path.name |
| font = cv2.FONT_HERSHEY_SIMPLEX |
| font_scale = 0.4 |
| thickness = 1 |
| text_color = (0, 0, 0) |
| |
| |
| 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) |
| |
| |
| 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: |
| |
| 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] |
| |
| |
| 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) |
| |
| |
| display_image = grid_image |
| h, w = grid_image.shape[:2] |
| print(f"Display size: {w}x{h} pixels") |
| |
| |
| window_name = f'Image Grid - Page {current_page + 1}/{num_pages}' |
| |
| |
| cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) |
| cv2.setWindowProperty(window_name, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) |
| cv2.imshow(window_name, display_image) |
| |
| |
| key = cv2.waitKey(0) & 0xFF |
| |
| |
| if key == ord('q') or key == 27: |
| print("\nQuitting...") |
| break |
| elif key == ord(' ') or key == 13: |
| if current_page < num_pages - 1: |
| current_page += 1 |
| cv2.destroyAllWindows() |
| else: |
| print("Already at last page!") |
| elif key == ord('b'): |
| if current_page > 0: |
| current_page -= 1 |
| cv2.destroyAllWindows() |
| else: |
| print("Already at first page!") |
| elif key == ord('s'): |
| 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'): |
| current_page = 0 |
| cv2.destroyAllWindows() |
| elif key == ord('0'): |
| current_page = num_pages - 1 |
| cv2.destroyAllWindows() |
| |
| cv2.destroyAllWindows() |
| print("Display complete!") |
|
|
| if __name__ == "__main__": |
| |
| image_directory = "min_max_segments" |
| |
| |
| display_images_opencv(image_directory, images_per_page=100, grid_size=(10, 10)) |