From bae760837cfe7fa228a0e30895bd7538d156dbf4 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 26 Sep 2025 10:59:15 +0200 Subject: [PATCH] Refactor display logic in MediaGrader to maintain aspect ratio and improve frame rendering This commit enhances the display functionality by calculating the available height for video timelines, resizing frames while preserving their aspect ratio, and centering them on a canvas. The window resizing logic has been updated to ensure the entire monitor space is utilized effectively, improving the visual presentation of media files. --- main.py | 61 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/main.py b/main.py index f410e09..4c1373d 100644 --- a/main.py +++ b/main.py @@ -159,30 +159,53 @@ class MediaGrader: # Get frame dimensions frame_height, frame_width = frame.shape[:2] - # Calculate aspect ratio - frame_aspect_ratio = frame_width / frame_height - monitor_aspect_ratio = self.MONITOR_WIDTH / self.MONITOR_HEIGHT + # Calculate available height (subtract timeline height for videos) + timeline_height = self.TIMELINE_HEIGHT if self.is_video(self.media_files[self.current_index]) else 0 + available_height = self.MONITOR_HEIGHT - timeline_height - # Determine if frame is vertical or horizontal relative to monitor - if frame_aspect_ratio < monitor_aspect_ratio: - # Frame is more vertical than monitor - maximize height - display_height = self.MONITOR_HEIGHT - display_width = int(display_height * frame_aspect_ratio) + # Calculate scale to fit within monitor bounds while maintaining aspect ratio + scale_x = self.MONITOR_WIDTH / frame_width + scale_y = available_height / frame_height + scale = min(scale_x, scale_y) + + # Calculate display dimensions + display_width = int(frame_width * scale) + display_height = int(frame_height * scale) + + # Resize the frame to maintain aspect ratio + if scale != 1.0: + resized_frame = cv2.resize(frame, (display_width, display_height), interpolation=cv2.INTER_AREA) else: - # Frame is more horizontal than monitor - maximize width - display_width = self.MONITOR_WIDTH - display_height = int(display_width / frame_aspect_ratio) + resized_frame = frame - # Resize window to calculated dimensions - cv2.resizeWindow("Media Grader", display_width, display_height) + # Create canvas with proper dimensions + canvas_height = self.MONITOR_HEIGHT + canvas_width = self.MONITOR_WIDTH + canvas = np.zeros((canvas_height, canvas_width, 3), dtype=np.uint8) + + # Center the resized frame on canvas + start_y = (available_height - display_height) // 2 + start_x = (self.MONITOR_WIDTH - display_width) // 2 + + # Ensure frame fits within canvas bounds + end_y = min(start_y + display_height, available_height) + end_x = min(start_x + display_width, self.MONITOR_WIDTH) + actual_height = end_y - start_y + actual_width = end_x - start_x + + if actual_height > 0 and actual_width > 0: + canvas[start_y:end_y, start_x:end_x] = resized_frame[:actual_height, :actual_width] + + # Resize window to full monitor size + cv2.resizeWindow("Media Grader", self.MONITOR_WIDTH, self.MONITOR_HEIGHT) # Center the window on screen - x_position = (self.MONITOR_WIDTH - display_width) // 2 - y_position = (self.MONITOR_HEIGHT - display_height) // 2 + x_position = 0 + y_position = 0 cv2.moveWindow("Media Grader", x_position, y_position) - # Display the frame - cv2.imshow("Media Grader", frame) + # Display the canvas with properly aspect-ratioed frame + cv2.imshow("Media Grader", canvas) def find_media_files(self) -> List[Path]: """Find all media files recursively in the directory""" @@ -697,7 +720,7 @@ class MediaGrader: # Draw timeline self.draw_timeline(frame) - # Maintain aspect ratio when displaying + # Display with proper aspect ratio self.display_with_aspect_ratio(frame) def display_multi_segment_frame(self): @@ -814,7 +837,7 @@ class MediaGrader: # Draw multi-segment timeline self.draw_multi_segment_timeline(combined_frame) - # Maintain aspect ratio when displaying + # Display with proper aspect ratio self.display_with_aspect_ratio(combined_frame) def draw_multi_segment_timeline(self, frame):