|
6 | 6 | """ |
7 | 7 |
|
8 | 8 | import asyncio |
9 | | -from io import BytesIO |
| 9 | +import enum |
10 | 10 |
|
11 | 11 | import brotli |
12 | | -from PIL import Image |
| 12 | +import cv2 |
| 13 | +import numpy as np |
13 | 14 |
|
14 | 15 | from resolver_athena_client.client.consts import EXPECTED_HEIGHT, EXPECTED_WIDTH |
15 | 16 | from resolver_athena_client.client.models import ImageData |
16 | 17 | from resolver_athena_client.generated.athena.models_pb2 import ImageFormat |
17 | | -import cv2 |
18 | | -import numpy as np |
19 | 18 |
|
20 | 19 | # Global optimization constants |
21 | 20 | _target_size = (EXPECTED_WIDTH, EXPECTED_HEIGHT) |
22 | 21 | _expected_raw_size = EXPECTED_WIDTH * EXPECTED_HEIGHT * 3 |
23 | 22 |
|
24 | 23 |
|
| 24 | +class OpenCVResamplingAlgorithm(enum.Enum): |
| 25 | + """Open CV Resampling Configuration. |
| 26 | +
|
| 27 | + Enum for ease of configuration and type-safety when selecting OpenCV |
| 28 | + resampling algorithms. |
| 29 | + """ |
| 30 | + |
| 31 | + NEAREST = cv2.INTER_NEAREST |
| 32 | + BOX = cv2.INTER_AREA # Best match for PIL's BOX |
| 33 | + BILINEAR = cv2.INTER_LINEAR |
| 34 | + LANCZOS = cv2.INTER_LANCZOS4 |
| 35 | + |
| 36 | + |
25 | 37 | def _is_raw_bgr_expected_size(data: bytes) -> bool: |
26 | 38 | """Detect if data is already a raw BGR array of expected size.""" |
27 | 39 | return len(data) == _expected_raw_size |
28 | 40 |
|
29 | 41 |
|
30 | 42 | async def resize_image( |
31 | 43 | image_data: ImageData, |
32 | | - sampling_algorithm: Image.Resampling = Image.Resampling.LANCZOS, |
| 44 | + sampling_algorithm: OpenCVResamplingAlgorithm = ( |
| 45 | + OpenCVResamplingAlgorithm.BILINEAR |
| 46 | + ), |
33 | 47 | ) -> ImageData: |
34 | 48 | """Resize an image to expected dimensions. |
35 | 49 |
|
@@ -62,7 +76,7 @@ def process_image() -> tuple[bytes, bool]: |
62 | 76 | resized_img = img |
63 | 77 | else: |
64 | 78 | resized_img = cv2.resize( |
65 | | - img, _target_size, interpolation=cv2.INTER_LINEAR |
| 79 | + img, _target_size, interpolation=sampling_algorithm.value |
66 | 80 | ) |
67 | 81 |
|
68 | 82 | # openCV loads in BGR format by default, so we can directly convert to |
|
0 commit comments