File size: 5,373 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 | #!/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)) |