Skip to content

Commit edc1301

Browse files
author
anna-singleton-resolver
committed
fix: linting and enum for selecting openCV resampling algos
1 parent 81d389d commit edc1301

2 files changed

Lines changed: 26 additions & 9 deletions

File tree

src/resolver_athena_client/client/athena_options.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
from dataclasses import dataclass
44

5-
from PIL.Image import Resampling
6-
75
from resolver_athena_client.client.correlation import (
86
CorrelationProvider,
97
HashCorrelationProvider,
108
)
9+
from resolver_athena_client.client.transformers.core import (
10+
OpenCVResamplingAlgorithm,
11+
)
1112

1213

1314
@dataclass
@@ -69,4 +70,6 @@ class AthenaOptions:
6970
timeout: float | None = 120.0
7071
keepalive_interval: float | None = None
7172
compression_quality: int = 11 # Brotli quality level (0-11)
72-
resampling_algorithm: Resampling = Resampling.LANCZOS
73+
resampling_algorithm: OpenCVResamplingAlgorithm = (
74+
OpenCVResamplingAlgorithm.BILINEAR
75+
)

src/resolver_athena_client/client/transformers/core.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,44 @@
66
"""
77

88
import asyncio
9-
from io import BytesIO
9+
import enum
1010

1111
import brotli
12-
from PIL import Image
12+
import cv2
13+
import numpy as np
1314

1415
from resolver_athena_client.client.consts import EXPECTED_HEIGHT, EXPECTED_WIDTH
1516
from resolver_athena_client.client.models import ImageData
1617
from resolver_athena_client.generated.athena.models_pb2 import ImageFormat
17-
import cv2
18-
import numpy as np
1918

2019
# Global optimization constants
2120
_target_size = (EXPECTED_WIDTH, EXPECTED_HEIGHT)
2221
_expected_raw_size = EXPECTED_WIDTH * EXPECTED_HEIGHT * 3
2322

2423

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+
2537
def _is_raw_bgr_expected_size(data: bytes) -> bool:
2638
"""Detect if data is already a raw BGR array of expected size."""
2739
return len(data) == _expected_raw_size
2840

2941

3042
async def resize_image(
3143
image_data: ImageData,
32-
sampling_algorithm: Image.Resampling = Image.Resampling.LANCZOS,
44+
sampling_algorithm: OpenCVResamplingAlgorithm = (
45+
OpenCVResamplingAlgorithm.BILINEAR
46+
),
3347
) -> ImageData:
3448
"""Resize an image to expected dimensions.
3549
@@ -62,7 +76,7 @@ def process_image() -> tuple[bytes, bool]:
6276
resized_img = img
6377
else:
6478
resized_img = cv2.resize(
65-
img, _target_size, interpolation=cv2.INTER_LINEAR
79+
img, _target_size, interpolation=sampling_algorithm.value
6680
)
6781

6882
# openCV loads in BGR format by default, so we can directly convert to

0 commit comments

Comments
 (0)