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.
This commit is contained in:
2025-09-26 10:59:15 +02:00
parent 4a1649a568
commit bae760837c

61
main.py
View File

@@ -159,30 +159,53 @@ class MediaGrader:
# Get frame dimensions # Get frame dimensions
frame_height, frame_width = frame.shape[:2] frame_height, frame_width = frame.shape[:2]
# Calculate aspect ratio # Calculate available height (subtract timeline height for videos)
frame_aspect_ratio = frame_width / frame_height timeline_height = self.TIMELINE_HEIGHT if self.is_video(self.media_files[self.current_index]) else 0
monitor_aspect_ratio = self.MONITOR_WIDTH / self.MONITOR_HEIGHT available_height = self.MONITOR_HEIGHT - timeline_height
# Determine if frame is vertical or horizontal relative to monitor # Calculate scale to fit within monitor bounds while maintaining aspect ratio
if frame_aspect_ratio < monitor_aspect_ratio: scale_x = self.MONITOR_WIDTH / frame_width
# Frame is more vertical than monitor - maximize height scale_y = available_height / frame_height
display_height = self.MONITOR_HEIGHT scale = min(scale_x, scale_y)
display_width = int(display_height * frame_aspect_ratio)
# 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: else:
# Frame is more horizontal than monitor - maximize width resized_frame = frame
display_width = self.MONITOR_WIDTH
display_height = int(display_width / frame_aspect_ratio)
# Resize window to calculated dimensions # Create canvas with proper dimensions
cv2.resizeWindow("Media Grader", display_width, display_height) 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 # Center the window on screen
x_position = (self.MONITOR_WIDTH - display_width) // 2 x_position = 0
y_position = (self.MONITOR_HEIGHT - display_height) // 2 y_position = 0
cv2.moveWindow("Media Grader", x_position, y_position) cv2.moveWindow("Media Grader", x_position, y_position)
# Display the frame # Display the canvas with properly aspect-ratioed frame
cv2.imshow("Media Grader", frame) cv2.imshow("Media Grader", canvas)
def find_media_files(self) -> List[Path]: def find_media_files(self) -> List[Path]:
"""Find all media files recursively in the directory""" """Find all media files recursively in the directory"""
@@ -697,7 +720,7 @@ class MediaGrader:
# Draw timeline # Draw timeline
self.draw_timeline(frame) self.draw_timeline(frame)
# Maintain aspect ratio when displaying # Display with proper aspect ratio
self.display_with_aspect_ratio(frame) self.display_with_aspect_ratio(frame)
def display_multi_segment_frame(self): def display_multi_segment_frame(self):
@@ -814,7 +837,7 @@ class MediaGrader:
# Draw multi-segment timeline # Draw multi-segment timeline
self.draw_multi_segment_timeline(combined_frame) self.draw_multi_segment_timeline(combined_frame)
# Maintain aspect ratio when displaying # Display with proper aspect ratio
self.display_with_aspect_ratio(combined_frame) self.display_with_aspect_ratio(combined_frame)
def draw_multi_segment_timeline(self, frame): def draw_multi_segment_timeline(self, frame):