From e957a7f4ddebf8d8b25205f799233b9ee125118e Mon Sep 17 00:00:00 2001 From: Max Buckley Date: Wed, 22 Apr 2026 13:31:11 +0200 Subject: [PATCH] =?UTF-8?q?Move=20BGR=E2=86=92RGB=20after=20resize=20in=20?= =?UTF-8?q?preview=20display=20path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The processing thread was running cvtColor on the full-resolution 1920×1080 frame before queueing it for display. Since the display thread immediately resizes the frame to the preview window (~5× smaller pixel count), doing the colour conversion on the resized buffer is cheaper overall. Processing thread now queues BGR; display thread resizes then cvtColor. --- modules/ui.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/modules/ui.py b/modules/ui.py index 0fe5990..a7cacf8 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -1220,11 +1220,9 @@ def _processing_thread_func(capture_queue, processed_queue, stop_event, 2, ) - # BGR→RGB in the processing thread so the display thread gets - # a contiguous RGB array (faster PIL.fromarray). - temp_frame = cv2.cvtColor(temp_frame, cv2.COLOR_BGR2RGB) - - # Put processed frame into output queue, dropping old frames if full + # Queue the processed frame as BGR; the display thread resizes to the + # preview window first and then runs cvtColor on the (much smaller) + # buffer — cheaper than converting the full 1080p frame here. try: processed_queue.put_nowait(temp_frame) except queue.Full: @@ -1294,15 +1292,17 @@ def create_webcam_preview(camera_index: int): return try: - rgb_frame = processed_queue.get_nowait() + bgr_frame = processed_queue.get_nowait() except queue.Empty: ROOT.after(poll_ms, _display_next_frame) return - # Frame is already RGB from processing thread; resize to preview window - rgb_frame = fit_image_to_size( - rgb_frame, PREVIEW.winfo_width(), PREVIEW.winfo_height() + # Resize the full-resolution BGR frame to the preview window first, + # then convert colour on the smaller buffer. + bgr_frame = fit_image_to_size( + bgr_frame, PREVIEW.winfo_width(), PREVIEW.winfo_height() ) + rgb_frame = cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(rgb_frame) image = ctk.CTkImage(image, size=image.size) preview_label.configure(image=image)