1919_expected_raw_size = EXPECTED_WIDTH * EXPECTED_HEIGHT * 3
2020
2121
22- def _is_raw_rgb_expected_size (data : bytes ) -> bool :
22+ def _is_raw_brg_expected_size (data : bytes ) -> bool :
2323 """Detect if data is already a raw RGB array of expected size."""
2424 return len (data ) == _expected_raw_size
2525
@@ -37,7 +37,7 @@ async def resize_image(image_data: ImageData) -> ImageData:
3737
3838 def process_image () -> tuple [bytes , bool ]:
3939 # Fast path for raw RGB arrays of correct size
40- if _is_raw_rgb_expected_size (image_data .data ):
40+ if _is_raw_brg_expected_size (image_data .data ):
4141 return image_data .data , False # No transformation needed
4242
4343 # Try to load the image data directly
@@ -55,8 +55,17 @@ def process_image() -> tuple[bytes, bool]:
5555 else :
5656 resized_image = rgb_image
5757
58- # Convert to raw RGB bytes (C-order: height x width x channels)
59- return resized_image .tobytes (), True # Data was transformed
58+ rgb_bytes = resized_image .tobytes ()
59+
60+ # Convert RGB to BGR by swapping channels
61+ brg_bytes = bytearray (len (rgb_bytes ))
62+ brg_bytes [:] = rgb_bytes # Initialize with RGB data
63+
64+ for i in range (0 , len (rgb_bytes ), 3 ):
65+ brg_bytes [i ] = rgb_bytes [i + 2 ] # B
66+ brg_bytes [i + 2 ] = rgb_bytes [i ] # R
67+
68+ return bytes (brg_bytes ), True # Data was transformed
6069
6170 # Use thread pool for CPU-intensive processing
6271 resized_bytes , was_transformed = await asyncio .to_thread (process_image )
0 commit comments