farshid-mcp-imageprocessing 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ .venv/
2
+ __pycache__/
3
+ *.pyc
4
+ .farshid/
5
+
6
+ # Build artifacts
7
+ build/
8
+ dist/
9
+ *.egg-info/
10
+ .eggs/
11
+
12
+ # Test / runtime output
13
+ test_output/
14
+
15
+ # OS / editor cruft
16
+ .DS_Store
17
+ Thumbs.db
18
+ *.swp
19
+ .idea/
20
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Farshid Pirahansiah
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,200 @@
1
+ Metadata-Version: 2.4
2
+ Name: farshid-mcp-imageprocessing
3
+ Version: 0.2.1
4
+ Summary: Local offline OpenCV-based image-processing MCP server: webcam capture, image transforms, edges, contours, face/QR detection, video, and ~40 tools.
5
+ Project-URL: Homepage, https://github.com/pirahansiah/farshid-mcp-imageProcessing
6
+ Project-URL: Repository, https://github.com/pirahansiah/farshid-mcp-imageProcessing
7
+ Project-URL: Issues, https://github.com/pirahansiah/farshid-mcp-imageProcessing/issues
8
+ Author: Farshid Pirahansiah
9
+ License: MIT
10
+ License-File: LICENSE
11
+ Keywords: computer-vision,image-processing,mcp,model-context-protocol,opencv,webcam
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: MacOS :: MacOS X
16
+ Classifier: Operating System :: Microsoft :: Windows :: Windows 11
17
+ Classifier: Operating System :: POSIX :: Linux
18
+ Classifier: Programming Language :: Python :: 3
19
+ Classifier: Programming Language :: Python :: 3.10
20
+ Classifier: Programming Language :: Python :: 3.11
21
+ Classifier: Programming Language :: Python :: 3.12
22
+ Classifier: Programming Language :: Python :: 3.13
23
+ Classifier: Programming Language :: Python :: 3.14
24
+ Classifier: Topic :: Multimedia :: Graphics
25
+ Classifier: Topic :: Scientific/Engineering :: Image Processing
26
+ Requires-Python: >=3.10
27
+ Requires-Dist: mcp[cli]>=1.18.0
28
+ Requires-Dist: numpy>=1.26
29
+ Requires-Dist: opencv-contrib-python>=4.10.0
30
+ Description-Content-Type: text/markdown
31
+
32
+ <!-- mcp-name: io.github.pirahansiah/farshid-mcp-imageProcessing -->
33
+
34
+ # farshid-mcp-imageProcessing
35
+
36
+ A comprehensive **OpenCV image-processing MCP server** for VS Code Copilot
37
+ Agent Mode (or any MCP client). Exposes ~40 tools across webcam capture, image
38
+ I/O, transforms, color, filtering, edges, thresholding, morphology,
39
+ contours/shapes, feature matching, object detection (faces / eyes / bodies /
40
+ QR), drawing, image arithmetic, template matching, and video processing.
41
+
42
+ - **PyPI:** [`farshid-mcp-imageProcessing`](https://pypi.org/project/farshid-mcp-imageProcessing/)
43
+ - **MCP Registry:** `io.github.pirahansiah/farshid-mcp-imageProcessing`
44
+ - **Python:** 3.14+
45
+ - **OS:** latest Windows 11, latest macOS, latest mainstream Linux (Ubuntu 24.04+/Fedora 41+)
46
+
47
+ ## Install (PyPI)
48
+
49
+ ```bash
50
+ pip install farshid-mcp-imageProcessing
51
+ farshid-mcp-imageprocessing # runs the stdio MCP server
52
+ ```
53
+
54
+ ## Register in VS Code
55
+
56
+ Add this to your user or workspace `mcp.json`:
57
+
58
+ ```jsonc
59
+ {
60
+ "servers": {
61
+ "imageProcessing": {
62
+ "command": "farshid-mcp-imageprocessing",
63
+ "type": "stdio"
64
+ }
65
+ }
66
+ }
67
+ ```
68
+
69
+ Or, if you cloned the repo and want to run from source with the local `.venv`:
70
+
71
+ ```bash
72
+ git clone https://github.com/pirahansiah/farshid-mcp-imageProcessing
73
+ cd farshid-mcp-imageProcessing
74
+ # Windows (PowerShell):
75
+ py -3.14 -m venv .venv ; .\.venv\Scripts\Activate.ps1
76
+ # macOS / Linux:
77
+ python3.14 -m venv .venv && source .venv/bin/activate
78
+
79
+ pip install -U pip
80
+ pip install -e .
81
+ ```
82
+
83
+ `opencv-contrib-python` is used so the bundled Haar cascades and extra
84
+ algorithms are available.
85
+
86
+ ## Quick start: the `/cv` Copilot prompt
87
+
88
+ This repo ships a workspace prompt file at
89
+ [.github/prompts/cv.prompt.md](.github/prompts/cv.prompt.md). In VS Code
90
+ Copilot Chat (Agent mode), type:
91
+
92
+ ```
93
+ /cv take image from webcam and save it as gray scale 240 * 240
94
+ ```
95
+
96
+ The agent will call `webcam_save`, `image_to_grayscale`, and `image_resize`
97
+ from this server to produce the requested file under `./.farshid/cv/`.
98
+
99
+ ## Tool catalog
100
+
101
+ ### Webcam / capture
102
+ - `webcam_capture(camera_index=0)` → returns a PNG image
103
+ - `webcam_save(output_path="", camera_index=0)`
104
+ - `webcam_preview(camera_index=0, seconds=10)` (local desktop window)
105
+ - `webcam_record(output_path, seconds=5, camera_index=0, fps=20)`
106
+
107
+ ### Image I/O & info
108
+ - `image_show(path)` — return image to chat
109
+ - `image_info(path)` — shape, dtype, mean, file size
110
+ - `image_convert(input_path, output_path, quality=95)`
111
+
112
+ ### Geometric transforms
113
+ - `image_resize(... width|height|scale, interpolation)`
114
+ - `image_crop(input_path, output_path, x, y, width, height)`
115
+ - `image_rotate(input_path, output_path, angle, scale=1, keep_size=False)`
116
+ - `image_flip(input_path, output_path, direction)`
117
+ - `image_pad(... top, bottom, left, right, border_type, color)`
118
+
119
+ ### Color
120
+ - `image_to_grayscale`
121
+ - `color_convert(target=gray|hsv|hls|lab|ycrcb|rgb|bgr)`
122
+ - `adjust_brightness_contrast`
123
+ - `histogram_equalize(method=clahe|global)`
124
+ - `histogram_data(bins=32)`
125
+
126
+ ### Filtering
127
+ - `blur_gaussian(ksize, sigma)`
128
+ - `blur_median(ksize)`
129
+ - `blur_bilateral(d, sigma_color, sigma_space)`
130
+ - `sharpen(amount)`
131
+ - `denoise(strength)`
132
+
133
+ ### Edges / gradients
134
+ - `edges_canny(threshold1, threshold2)`
135
+ - `edges_sobel(ksize)`
136
+ - `edges_laplacian(ksize)`
137
+
138
+ ### Thresholding & morphology
139
+ - `threshold(method=otsu|binary|binary_inv|adaptive_mean|adaptive_gaussian)`
140
+ - `morphology(op=erode|dilate|open|close|gradient|tophat|blackhat)`
141
+
142
+ ### Contours & shapes
143
+ - `find_contours(input_path, output_path?, thresh, min_area)`
144
+ - `detect_circles(...)` — Hough
145
+ - `detect_lines(...)` — Probabilistic Hough
146
+ - `detect_corners(...)` — Shi-Tomasi
147
+
148
+ ### Feature matching
149
+ - `feature_match(image1, image2, output_path?)` — ORB + BFMatcher
150
+
151
+ ### Object detection (Haar)
152
+ - `detect_faces`
153
+ - `detect_eyes`
154
+ - `detect_bodies`
155
+ - `detect_qrcode`
156
+
157
+ ### Drawing
158
+ - `draw_rectangle`, `draw_circle`, `draw_line`, `draw_text`
159
+
160
+ ### Composition / arithmetic
161
+ - `image_blend(image1, image2, output_path, alpha)`
162
+ - `image_diff(image1, image2, output_path?)` → mean/max diff
163
+ - `image_concat(images, output_path, direction)`
164
+ - `template_match(image_path, template_path, output_path?, threshold)`
165
+
166
+ ### Video
167
+ - `video_info(path)`
168
+ - `video_extract_frames(video_path, output_dir, every_n, max_frames, ext)`
169
+ - `video_thumbnail(video_path, output_path, time_seconds)`
170
+
171
+ ## Build & publish
172
+
173
+ ```bash
174
+ pip install -U build twine mcp-publisher
175
+ python -m build
176
+ twine upload dist/*
177
+ mcp-publisher login github
178
+ mcp-publisher publish .mcp/server.json
179
+ ```
180
+
181
+ ## OS notes
182
+
183
+ - **Windows 11 (latest):** webcam works out of the box; ensure *Settings →
184
+ Privacy & security → Camera → Let desktop apps access your camera* is **On**.
185
+ - **macOS (latest):** the first webcam call triggers a system Camera
186
+ permission prompt; grant it to the terminal/VS Code process.
187
+ - **Linux (latest):** requires a working `/dev/video*` device. Headless
188
+ servers without a display cannot use `webcam_preview` (it opens an OpenCV
189
+ window).
190
+
191
+ ## Notes
192
+
193
+ - Never use `print()` in tool functions: stdout is the MCP protocol channel.
194
+ Use `sys.stderr` (the `_log` helper at the bottom of `server.py`).
195
+ - `webcam_preview` opens a real desktop window — only works where the server
196
+ has a display (not over plain SSH or in a headless container).
197
+ - All paths support `~` expansion. Output directories are created
198
+ automatically.
199
+ - Tools that return annotated images take an optional `output_path`; when
200
+ omitted they only return the JSON metadata.
@@ -0,0 +1,169 @@
1
+ <!-- mcp-name: io.github.pirahansiah/farshid-mcp-imageProcessing -->
2
+
3
+ # farshid-mcp-imageProcessing
4
+
5
+ A comprehensive **OpenCV image-processing MCP server** for VS Code Copilot
6
+ Agent Mode (or any MCP client). Exposes ~40 tools across webcam capture, image
7
+ I/O, transforms, color, filtering, edges, thresholding, morphology,
8
+ contours/shapes, feature matching, object detection (faces / eyes / bodies /
9
+ QR), drawing, image arithmetic, template matching, and video processing.
10
+
11
+ - **PyPI:** [`farshid-mcp-imageProcessing`](https://pypi.org/project/farshid-mcp-imageProcessing/)
12
+ - **MCP Registry:** `io.github.pirahansiah/farshid-mcp-imageProcessing`
13
+ - **Python:** 3.14+
14
+ - **OS:** latest Windows 11, latest macOS, latest mainstream Linux (Ubuntu 24.04+/Fedora 41+)
15
+
16
+ ## Install (PyPI)
17
+
18
+ ```bash
19
+ pip install farshid-mcp-imageProcessing
20
+ farshid-mcp-imageprocessing # runs the stdio MCP server
21
+ ```
22
+
23
+ ## Register in VS Code
24
+
25
+ Add this to your user or workspace `mcp.json`:
26
+
27
+ ```jsonc
28
+ {
29
+ "servers": {
30
+ "imageProcessing": {
31
+ "command": "farshid-mcp-imageprocessing",
32
+ "type": "stdio"
33
+ }
34
+ }
35
+ }
36
+ ```
37
+
38
+ Or, if you cloned the repo and want to run from source with the local `.venv`:
39
+
40
+ ```bash
41
+ git clone https://github.com/pirahansiah/farshid-mcp-imageProcessing
42
+ cd farshid-mcp-imageProcessing
43
+ # Windows (PowerShell):
44
+ py -3.14 -m venv .venv ; .\.venv\Scripts\Activate.ps1
45
+ # macOS / Linux:
46
+ python3.14 -m venv .venv && source .venv/bin/activate
47
+
48
+ pip install -U pip
49
+ pip install -e .
50
+ ```
51
+
52
+ `opencv-contrib-python` is used so the bundled Haar cascades and extra
53
+ algorithms are available.
54
+
55
+ ## Quick start: the `/cv` Copilot prompt
56
+
57
+ This repo ships a workspace prompt file at
58
+ [.github/prompts/cv.prompt.md](.github/prompts/cv.prompt.md). In VS Code
59
+ Copilot Chat (Agent mode), type:
60
+
61
+ ```
62
+ /cv take image from webcam and save it as gray scale 240 * 240
63
+ ```
64
+
65
+ The agent will call `webcam_save`, `image_to_grayscale`, and `image_resize`
66
+ from this server to produce the requested file under `./.farshid/cv/`.
67
+
68
+ ## Tool catalog
69
+
70
+ ### Webcam / capture
71
+ - `webcam_capture(camera_index=0)` → returns a PNG image
72
+ - `webcam_save(output_path="", camera_index=0)`
73
+ - `webcam_preview(camera_index=0, seconds=10)` (local desktop window)
74
+ - `webcam_record(output_path, seconds=5, camera_index=0, fps=20)`
75
+
76
+ ### Image I/O & info
77
+ - `image_show(path)` — return image to chat
78
+ - `image_info(path)` — shape, dtype, mean, file size
79
+ - `image_convert(input_path, output_path, quality=95)`
80
+
81
+ ### Geometric transforms
82
+ - `image_resize(... width|height|scale, interpolation)`
83
+ - `image_crop(input_path, output_path, x, y, width, height)`
84
+ - `image_rotate(input_path, output_path, angle, scale=1, keep_size=False)`
85
+ - `image_flip(input_path, output_path, direction)`
86
+ - `image_pad(... top, bottom, left, right, border_type, color)`
87
+
88
+ ### Color
89
+ - `image_to_grayscale`
90
+ - `color_convert(target=gray|hsv|hls|lab|ycrcb|rgb|bgr)`
91
+ - `adjust_brightness_contrast`
92
+ - `histogram_equalize(method=clahe|global)`
93
+ - `histogram_data(bins=32)`
94
+
95
+ ### Filtering
96
+ - `blur_gaussian(ksize, sigma)`
97
+ - `blur_median(ksize)`
98
+ - `blur_bilateral(d, sigma_color, sigma_space)`
99
+ - `sharpen(amount)`
100
+ - `denoise(strength)`
101
+
102
+ ### Edges / gradients
103
+ - `edges_canny(threshold1, threshold2)`
104
+ - `edges_sobel(ksize)`
105
+ - `edges_laplacian(ksize)`
106
+
107
+ ### Thresholding & morphology
108
+ - `threshold(method=otsu|binary|binary_inv|adaptive_mean|adaptive_gaussian)`
109
+ - `morphology(op=erode|dilate|open|close|gradient|tophat|blackhat)`
110
+
111
+ ### Contours & shapes
112
+ - `find_contours(input_path, output_path?, thresh, min_area)`
113
+ - `detect_circles(...)` — Hough
114
+ - `detect_lines(...)` — Probabilistic Hough
115
+ - `detect_corners(...)` — Shi-Tomasi
116
+
117
+ ### Feature matching
118
+ - `feature_match(image1, image2, output_path?)` — ORB + BFMatcher
119
+
120
+ ### Object detection (Haar)
121
+ - `detect_faces`
122
+ - `detect_eyes`
123
+ - `detect_bodies`
124
+ - `detect_qrcode`
125
+
126
+ ### Drawing
127
+ - `draw_rectangle`, `draw_circle`, `draw_line`, `draw_text`
128
+
129
+ ### Composition / arithmetic
130
+ - `image_blend(image1, image2, output_path, alpha)`
131
+ - `image_diff(image1, image2, output_path?)` → mean/max diff
132
+ - `image_concat(images, output_path, direction)`
133
+ - `template_match(image_path, template_path, output_path?, threshold)`
134
+
135
+ ### Video
136
+ - `video_info(path)`
137
+ - `video_extract_frames(video_path, output_dir, every_n, max_frames, ext)`
138
+ - `video_thumbnail(video_path, output_path, time_seconds)`
139
+
140
+ ## Build & publish
141
+
142
+ ```bash
143
+ pip install -U build twine mcp-publisher
144
+ python -m build
145
+ twine upload dist/*
146
+ mcp-publisher login github
147
+ mcp-publisher publish .mcp/server.json
148
+ ```
149
+
150
+ ## OS notes
151
+
152
+ - **Windows 11 (latest):** webcam works out of the box; ensure *Settings →
153
+ Privacy & security → Camera → Let desktop apps access your camera* is **On**.
154
+ - **macOS (latest):** the first webcam call triggers a system Camera
155
+ permission prompt; grant it to the terminal/VS Code process.
156
+ - **Linux (latest):** requires a working `/dev/video*` device. Headless
157
+ servers without a display cannot use `webcam_preview` (it opens an OpenCV
158
+ window).
159
+
160
+ ## Notes
161
+
162
+ - Never use `print()` in tool functions: stdout is the MCP protocol channel.
163
+ Use `sys.stderr` (the `_log` helper at the bottom of `server.py`).
164
+ - `webcam_preview` opens a real desktop window — only works where the server
165
+ has a display (not over plain SSH or in a headless container).
166
+ - All paths support `~` expansion. Output directories are created
167
+ automatically.
168
+ - Tools that return annotated images take an optional `output_path`; when
169
+ omitted they only return the JSON metadata.
@@ -0,0 +1,55 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.27"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "farshid-mcp-imageprocessing"
7
+ version = "0.2.1"
8
+ description = "Local offline OpenCV-based image-processing MCP server: webcam capture, image transforms, edges, contours, face/QR detection, video, and ~40 tools."
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = { text = "MIT" }
12
+ authors = [
13
+ { name = "Farshid Pirahansiah" }
14
+ ]
15
+ keywords = ["mcp", "model-context-protocol", "opencv", "computer-vision", "image-processing", "webcam"]
16
+ classifiers = [
17
+ "Development Status :: 4 - Beta",
18
+ "Intended Audience :: Developers",
19
+ "License :: OSI Approved :: MIT License",
20
+ "Operating System :: Microsoft :: Windows :: Windows 11",
21
+ "Operating System :: MacOS :: MacOS X",
22
+ "Operating System :: POSIX :: Linux",
23
+ "Programming Language :: Python :: 3",
24
+ "Programming Language :: Python :: 3.10",
25
+ "Programming Language :: Python :: 3.11",
26
+ "Programming Language :: Python :: 3.12",
27
+ "Programming Language :: Python :: 3.13",
28
+ "Programming Language :: Python :: 3.14",
29
+ "Topic :: Multimedia :: Graphics",
30
+ "Topic :: Scientific/Engineering :: Image Processing",
31
+ ]
32
+ dependencies = [
33
+ "mcp[cli]>=1.18.0",
34
+ "opencv-contrib-python>=4.10.0",
35
+ "numpy>=1.26",
36
+ ]
37
+
38
+ [project.urls]
39
+ Homepage = "https://github.com/pirahansiah/farshid-mcp-imageProcessing"
40
+ Repository = "https://github.com/pirahansiah/farshid-mcp-imageProcessing"
41
+ Issues = "https://github.com/pirahansiah/farshid-mcp-imageProcessing/issues"
42
+
43
+ [project.scripts]
44
+ farshid-mcp-imageprocessing = "farshid_mcp_imageprocessing.server:main"
45
+
46
+ [tool.hatch.build.targets.wheel]
47
+ packages = ["src/farshid_mcp_imageprocessing"]
48
+
49
+ [tool.hatch.build.targets.sdist]
50
+ include = [
51
+ "src/farshid_mcp_imageprocessing",
52
+ "README.md",
53
+ "LICENSE",
54
+ "pyproject.toml",
55
+ ]
@@ -0,0 +1,2 @@
1
+ """farshid-mcp-imageProcessing — local offline OpenCV image-processing MCP server."""
2
+ __version__ = "0.2.0"
@@ -0,0 +1,100 @@
1
+ """Shared helpers for the OpenCV MCP server."""
2
+ from __future__ import annotations
3
+
4
+ from pathlib import Path
5
+ from typing import Tuple
6
+
7
+ import cv2
8
+ import numpy as np
9
+ from mcp.server.fastmcp import Image
10
+
11
+
12
+ # ---------- I/O ----------
13
+
14
+ def read_image(path: str, flag: int = cv2.IMREAD_UNCHANGED) -> np.ndarray:
15
+ """Read an image from disk, raising a clear error on failure."""
16
+ p = Path(path).expanduser()
17
+ if not p.exists():
18
+ raise FileNotFoundError(f"Image not found: {p}")
19
+ img = cv2.imread(str(p), flag)
20
+ if img is None:
21
+ raise RuntimeError(f"OpenCV could not decode image: {p}")
22
+ return img
23
+
24
+
25
+ def write_image(path: str, img: np.ndarray) -> Path:
26
+ """Write an image, creating parent dirs."""
27
+ p = Path(path).expanduser()
28
+ p.parent.mkdir(parents=True, exist_ok=True)
29
+ ok = cv2.imwrite(str(p), img)
30
+ if not ok:
31
+ raise RuntimeError(f"OpenCV could not write image: {p}")
32
+ return p
33
+
34
+
35
+ def to_mcp_image(img: np.ndarray, fmt: str = "png") -> Image:
36
+ """Encode an ndarray as an MCP Image payload."""
37
+ fmt = fmt.lower().lstrip(".")
38
+ ext = "." + fmt
39
+ ok, buf = cv2.imencode(ext, img)
40
+ if not ok:
41
+ raise RuntimeError(f"Could not encode image as {fmt}")
42
+ return Image(data=buf.tobytes(), format=fmt)
43
+
44
+
45
+ # ---------- Webcam ----------
46
+
47
+ def grab_frame(camera_index: int = 0, warmup_frames: int = 2) -> np.ndarray:
48
+ """Open webcam, discard a few warmup frames, return one frame."""
49
+ cap = cv2.VideoCapture(camera_index)
50
+ try:
51
+ if not cap.isOpened():
52
+ raise RuntimeError(
53
+ f"Could not open webcam at camera_index={camera_index}. "
54
+ "Check OS camera permissions and whether another app is using it."
55
+ )
56
+ frame = None
57
+ for _ in range(max(1, warmup_frames + 1)):
58
+ ok, frame = cap.read()
59
+ if not ok or frame is None:
60
+ raise RuntimeError("Webcam opened, but no frame could be read.")
61
+ return frame
62
+ finally:
63
+ cap.release()
64
+
65
+
66
+ # ---------- Geometry / parsing ----------
67
+
68
+ def parse_color(color: "str | Tuple[int, int, int]") -> Tuple[int, int, int]:
69
+ """Accept '#rrggbb', 'r,g,b', or tuple. Returns BGR for OpenCV."""
70
+ if isinstance(color, (tuple, list)) and len(color) == 3:
71
+ r, g, b = [int(c) for c in color]
72
+ return (b, g, r)
73
+ s = str(color).strip()
74
+ if s.startswith("#") and len(s) == 7:
75
+ r = int(s[1:3], 16); g = int(s[3:5], 16); b = int(s[5:7], 16)
76
+ return (b, g, r)
77
+ parts = [p.strip() for p in s.split(",")]
78
+ if len(parts) == 3:
79
+ r, g, b = [int(p) for p in parts]
80
+ return (b, g, r)
81
+ raise ValueError(f"Cannot parse color: {color!r}")
82
+
83
+
84
+ def ensure_bgr(img: np.ndarray) -> np.ndarray:
85
+ """Convert grayscale or BGRA to 3-channel BGR."""
86
+ if img.ndim == 2:
87
+ return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
88
+ if img.shape[2] == 4:
89
+ return cv2.cvtColor(img, cv2.COLOR_BGRA2BGR)
90
+ return img
91
+
92
+
93
+ def odd(n: int, minimum: int = 1) -> int:
94
+ """Force value to an odd integer >= minimum (kernel sizes need odd)."""
95
+ n = int(n)
96
+ if n < minimum:
97
+ n = minimum
98
+ if n % 2 == 0:
99
+ n += 1
100
+ return n