refactor(croppa): adjust draw_crop_overlay to use canvas and screen coordinates

This commit is contained in:
2025-08-19 09:47:28 +02:00
parent cdad9198bc
commit 811cd43261

View File

@@ -200,21 +200,38 @@ class VideoEditor:
cv2.line(frame, (cut_end_x, bar_y), (cut_end_x, bar_y + self.TIMELINE_BAR_HEIGHT), self.TIMELINE_COLOR_CUT_POINT, 3) cv2.line(frame, (cut_end_x, bar_y), (cut_end_x, bar_y + self.TIMELINE_BAR_HEIGHT), self.TIMELINE_COLOR_CUT_POINT, 3)
cv2.putText(frame, "2", (cut_end_x - 5, bar_y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, self.TIMELINE_COLOR_CUT_POINT, 1) cv2.putText(frame, "2", (cut_end_x - 5, bar_y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.4, self.TIMELINE_COLOR_CUT_POINT, 1)
def draw_crop_overlay(self, frame): def draw_crop_overlay(self, canvas, start_x, start_y, frame_width, frame_height):
"""Draw crop selection overlay""" """Draw crop overlay on canvas using screen coordinates"""
# Draw preview rectangle (green) - already in screen coordinates
if self.crop_preview_rect: if self.crop_preview_rect:
x, y, w, h = self.crop_preview_rect x, y, w, h = self.crop_preview_rect
# Draw rectangle cv2.rectangle(canvas, (int(x), int(y)), (int(x + w), int(y + h)), (0, 255, 0), 2)
cv2.rectangle(frame, (int(x), int(y)), (int(x + w), int(y + h)), (0, 255, 0), 2)
# Draw semi-transparent overlay outside crop area
overlay = frame.copy()
cv2.rectangle(overlay, (0, 0), (frame.shape[1], frame.shape[0]), (0, 0, 0), -1)
cv2.rectangle(overlay, (int(x), int(y)), (int(x + w), int(y + h)), (255, 255, 255), -1)
cv2.addWeighted(frame, 0.7, overlay, 0.3, 0, frame)
# Draw final crop rectangle (red) - convert from video to screen coordinates
if self.crop_rect: if self.crop_rect:
# Convert crop coordinates from original video to screen coordinates
x, y, w, h = self.crop_rect x, y, w, h = self.crop_rect
cv2.rectangle(frame, (int(x), int(y)), (int(x + w), int(y + h)), (255, 0, 0), 2)
# Apply the same scaling logic as in display_current_frame
original_height, original_width = self.current_display_frame.shape[:2]
available_height = self.window_height - self.TIMELINE_HEIGHT
scale = min(self.window_width / original_width, available_height / original_height)
if scale < 1.0:
new_width = int(original_width * scale)
new_height = int(original_height * scale)
else:
new_width = original_width
new_height = original_height
# Convert video coordinates to screen coordinates
screen_x = start_x + (x * new_width / original_width)
screen_y = start_y + (y * new_height / original_height)
screen_w = w * new_width / original_width
screen_h = h * new_height / original_height
cv2.rectangle(canvas, (int(screen_x), int(screen_y)),
(int(screen_x + screen_w), int(screen_y + screen_h)), (255, 0, 0), 2)
def display_current_frame(self): def display_current_frame(self):
"""Display the current frame with all overlays""" """Display the current frame with all overlays"""
@@ -247,9 +264,9 @@ class VideoEditor:
canvas[start_y:start_y + frame_height, start_x:start_x + frame_width] = display_frame canvas[start_y:start_y + frame_height, start_x:start_x + frame_width] = display_frame
# Draw crop overlay on original frame coordinates # Draw crop overlay
if not (self.crop_rect or self.crop_preview_rect): if self.crop_rect or self.crop_preview_rect:
self.draw_crop_overlay(canvas[start_y:start_y + frame_height, start_x:start_x + frame_width]) self.draw_crop_overlay(canvas, start_x, start_y, frame_width, frame_height)
# Add info overlay # Add info overlay
info_text = f"Frame: {self.current_frame}/{self.total_frames} | Speed: {self.playback_speed:.1f}x | Zoom: {self.zoom_factor:.1f}x | {'Playing' if self.is_playing else 'Paused'}" info_text = f"Frame: {self.current_frame}/{self.total_frames} | Speed: {self.playback_speed:.1f}x | Zoom: {self.zoom_factor:.1f}x | {'Playing' if self.is_playing else 'Paused'}"