From 6c862714287dd04b54666185de887ca438ce1573 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Wed, 17 Sep 2025 01:51:56 +0200 Subject: [PATCH] Refactor cropping logic in VideoEditor to utilize effective crop rectangle This commit updates the cropping functionality in the VideoEditor class to use an effective crop rectangle derived from the current frame. It ensures that the crop is applied correctly within the bounds of the processed frame, enhancing the accuracy of cropping after rotation. Additionally, a visual outline of the effective crop is drawn on the canvas for debugging purposes, improving the user experience during video editing. --- croppa/main.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/croppa/main.py b/croppa/main.py index 3f6fb00..d3d58f8 100644 --- a/croppa/main.py +++ b/croppa/main.py @@ -1103,16 +1103,14 @@ class VideoEditor: if self.rotation_angle != 0: processed_frame = self.apply_rotation(processed_frame) - # Apply crop (interpreted in rotated frame coordinates) - if self.crop_rect: - x, y, w, h = map(int, self.crop_rect) - # Ensure crop is within frame bounds (rotated frame) - x = max(0, min(x, processed_frame.shape[1] - 1)) - y = max(0, min(y, processed_frame.shape[0] - 1)) - w = min(w, processed_frame.shape[1] - x) - h = min(h, processed_frame.shape[0] - y) - if w > 0 and h > 0: - processed_frame = processed_frame[y : y + h, x : x + w] + # Apply crop (interpreted in rotated frame coordinates) using EFFECTIVE rect + eff_x, eff_y, eff_w, eff_h = self._get_effective_crop_rect_for_frame(getattr(self, 'current_frame', 0)) + if eff_w > 0 and eff_h > 0: + eff_x = max(0, min(eff_x, processed_frame.shape[1] - 1)) + eff_y = max(0, min(eff_y, processed_frame.shape[0] - 1)) + eff_w = min(eff_w, processed_frame.shape[1] - eff_x) + eff_h = min(eff_h, processed_frame.shape[0] - eff_y) + processed_frame = processed_frame[eff_y : eff_y + eff_h, eff_x : eff_x + eff_w] # Apply zoom if self.zoom_factor != 1.0: @@ -2024,6 +2022,12 @@ class VideoEditor: sx, sy = self._map_rotated_to_screen(interp[0], interp[1]) cv2.line(canvas, (sx - 10, sy), (sx + 10, sy), (255, 0, 0), 2) cv2.line(canvas, (sx, sy - 10), (sx, sy + 10), (255, 0, 0), 2) + # Draw a faint outline of the effective crop to confirm follow + eff_x, eff_y, eff_w, eff_h = self._get_effective_crop_rect_for_frame(self.current_frame) + # Map rotated crop corners to screen for debug outline + tlx, tly = self._map_rotated_to_screen(eff_x, eff_y) + brx, bry = self._map_rotated_to_screen(eff_x + eff_w, eff_y + eff_h) + cv2.rectangle(canvas, (tlx, tly), (brx, bry), (255, 0, 0), 1) # Draw timeline self.draw_timeline(canvas)