farshid-mcp-imageprocessing 0.2.1__py3-none-any.whl

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,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
@@ -0,0 +1,899 @@
1
+ """
2
+ farshid_mcp_imageprocessing.server — A comprehensive OpenCV image-processing MCP server.
3
+
4
+ Run as a stdio MCP server; do NOT use print() (stdout is the protocol channel).
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import sys
9
+ import time
10
+ from datetime import datetime
11
+ from pathlib import Path
12
+ from typing import List
13
+
14
+ import cv2
15
+ import numpy as np
16
+
17
+ from mcp.server.fastmcp import FastMCP, Image
18
+
19
+ from .cv_helpers import (
20
+ read_image,
21
+ write_image,
22
+ to_mcp_image,
23
+ grab_frame,
24
+ parse_color,
25
+ ensure_bgr,
26
+ odd,
27
+ )
28
+
29
+ # All generated artifacts (images, videos, data) are written under this folder.
30
+ DATA_DIR = Path.cwd() / ".farshid"
31
+
32
+ mcp = FastMCP("imageProcessing")
33
+
34
+ # ============================================================================
35
+ # 1. WEBCAM / CAPTURE
36
+ # ============================================================================
37
+
38
+ @mcp.tool()
39
+ def webcam_capture(camera_index: int = 0) -> Image:
40
+ """Capture one frame from the webcam and return it as a PNG image."""
41
+ frame = grab_frame(camera_index)
42
+ return to_mcp_image(frame, "png")
43
+
44
+
45
+ @mcp.tool()
46
+ def webcam_save(output_path: str = "", camera_index: int = 0) -> str:
47
+ """Capture a webcam frame and save to disk. If output_path is empty,
48
+ saves to ./.farshid/captures/snapshot_<timestamp>.png."""
49
+ frame = grab_frame(camera_index)
50
+ if not output_path:
51
+ out_dir = DATA_DIR / "captures"
52
+ out_dir.mkdir(parents=True, exist_ok=True)
53
+ output_path = str(out_dir / f"snapshot_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png")
54
+ p = write_image(output_path, frame)
55
+ return f"Saved webcam snapshot to: {p}"
56
+
57
+
58
+ @mcp.tool()
59
+ def webcam_preview(camera_index: int = 0, seconds: int = 10) -> str:
60
+ """Open a local OpenCV preview window for up to N seconds (max 120).
61
+ Press 'q' in the window to close early. Requires a local display."""
62
+ seconds = max(1, min(int(seconds), 120))
63
+ cap = cv2.VideoCapture(camera_index)
64
+ if not cap.isOpened():
65
+ raise RuntimeError(f"Could not open webcam at camera_index={camera_index}.")
66
+ win = "OpenCV MCP preview - press q to quit"
67
+ start = time.time()
68
+ try:
69
+ while time.time() - start < seconds:
70
+ ok, frame = cap.read()
71
+ if not ok:
72
+ break
73
+ cv2.imshow(win, frame)
74
+ if (cv2.waitKey(1) & 0xFF) == ord("q"):
75
+ break
76
+ finally:
77
+ cap.release()
78
+ cv2.destroyAllWindows()
79
+ return f"Closed preview after {round(time.time() - start, 1)}s."
80
+
81
+
82
+ @mcp.tool()
83
+ def webcam_record(output_path: str = "", seconds: int = 5,
84
+ camera_index: int = 0, fps: float = 20.0) -> str:
85
+ """Record a short MP4 from the webcam for N seconds (max 120).
86
+ If output_path is empty, saves to ./.farshid/videos/clip_<timestamp>.mp4."""
87
+ seconds = max(1, min(int(seconds), 120))
88
+ cap = cv2.VideoCapture(camera_index)
89
+ if not cap.isOpened():
90
+ raise RuntimeError(f"Could not open webcam at camera_index={camera_index}.")
91
+ ok, frame = cap.read()
92
+ if not ok:
93
+ cap.release()
94
+ raise RuntimeError("Webcam returned no frames.")
95
+ h, w = frame.shape[:2]
96
+ if not output_path:
97
+ out_dir = DATA_DIR / "videos"
98
+ out_dir.mkdir(parents=True, exist_ok=True)
99
+ output_path = str(out_dir / f"clip_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4")
100
+ p = Path(output_path).expanduser()
101
+ p.parent.mkdir(parents=True, exist_ok=True)
102
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v")
103
+ writer = cv2.VideoWriter(str(p), fourcc, float(fps), (w, h))
104
+ n = 0
105
+ start = time.time()
106
+ try:
107
+ while time.time() - start < seconds:
108
+ ok, frame = cap.read()
109
+ if not ok:
110
+ break
111
+ writer.write(frame)
112
+ n += 1
113
+ finally:
114
+ cap.release()
115
+ writer.release()
116
+ return f"Recorded {n} frames ({round(time.time() - start, 1)}s) to {p}"
117
+
118
+
119
+ # ============================================================================
120
+ # 2. IMAGE I/O AND INFO
121
+ # ============================================================================
122
+
123
+ @mcp.tool()
124
+ def image_show(path: str) -> Image:
125
+ """Load an image from disk and return it as PNG to the chat."""
126
+ img = read_image(path)
127
+ return to_mcp_image(ensure_bgr(img), "png")
128
+
129
+
130
+ @mcp.tool()
131
+ def image_info(path: str) -> dict:
132
+ """Return shape, dtype, channel count, file size, and basic stats."""
133
+ img = read_image(path)
134
+ p = Path(path).expanduser()
135
+ h, w = img.shape[:2]
136
+ ch = 1 if img.ndim == 2 else img.shape[2]
137
+ info = {
138
+ "path": str(p),
139
+ "file_size_bytes": p.stat().st_size,
140
+ "width": int(w),
141
+ "height": int(h),
142
+ "channels": int(ch),
143
+ "dtype": str(img.dtype),
144
+ "min": float(img.min()),
145
+ "max": float(img.max()),
146
+ "mean": float(img.mean()),
147
+ }
148
+ return info
149
+
150
+
151
+ @mcp.tool()
152
+ def image_convert(input_path: str, output_path: str, quality: int = 95) -> str:
153
+ """Convert an image between formats by extension (e.g. .jpg, .png, .webp, .bmp).
154
+ `quality` applies to JPEG/WebP."""
155
+ img = read_image(input_path)
156
+ p = Path(output_path).expanduser()
157
+ p.parent.mkdir(parents=True, exist_ok=True)
158
+ ext = p.suffix.lower()
159
+ params = []
160
+ if ext in (".jpg", ".jpeg"):
161
+ params = [cv2.IMWRITE_JPEG_QUALITY, int(quality)]
162
+ elif ext == ".webp":
163
+ params = [cv2.IMWRITE_WEBP_QUALITY, int(quality)]
164
+ elif ext == ".png":
165
+ params = [cv2.IMWRITE_PNG_COMPRESSION, 3]
166
+ ok = cv2.imwrite(str(p), img, params)
167
+ if not ok:
168
+ raise RuntimeError(f"Could not write {p}")
169
+ return f"Wrote {p} ({p.stat().st_size} bytes)"
170
+
171
+
172
+ # ============================================================================
173
+ # 3. GEOMETRIC TRANSFORMS
174
+ # ============================================================================
175
+
176
+ @mcp.tool()
177
+ def image_resize(input_path: str, output_path: str,
178
+ width: int = 0, height: int = 0,
179
+ scale: float = 0.0,
180
+ interpolation: str = "area") -> str:
181
+ """Resize an image. Provide either (width, height), or scale, or one of
182
+ width/height (the other is computed to keep aspect ratio).
183
+ interpolation: nearest, linear, cubic, area, lanczos."""
184
+ img = read_image(input_path)
185
+ h, w = img.shape[:2]
186
+ if scale and scale > 0:
187
+ nw, nh = int(w * scale), int(h * scale)
188
+ elif width and height:
189
+ nw, nh = int(width), int(height)
190
+ elif width:
191
+ nw = int(width); nh = int(h * (nw / w))
192
+ elif height:
193
+ nh = int(height); nw = int(w * (nh / h))
194
+ else:
195
+ raise ValueError("Provide width, height, or scale.")
196
+ interp_map = {
197
+ "nearest": cv2.INTER_NEAREST, "linear": cv2.INTER_LINEAR,
198
+ "cubic": cv2.INTER_CUBIC, "area": cv2.INTER_AREA,
199
+ "lanczos": cv2.INTER_LANCZOS4,
200
+ }
201
+ out = cv2.resize(img, (nw, nh), interpolation=interp_map.get(interpolation, cv2.INTER_AREA))
202
+ p = write_image(output_path, out)
203
+ return f"Resized {w}x{h} -> {nw}x{nh}, saved {p}"
204
+
205
+
206
+ @mcp.tool()
207
+ def image_crop(input_path: str, output_path: str,
208
+ x: int, y: int, width: int, height: int) -> str:
209
+ """Crop a rectangular region (x, y, width, height) from an image."""
210
+ img = read_image(input_path)
211
+ h, w = img.shape[:2]
212
+ x2, y2 = min(w, x + width), min(h, y + height)
213
+ x, y = max(0, x), max(0, y)
214
+ if x2 <= x or y2 <= y:
215
+ raise ValueError("Crop region is empty or out of bounds.")
216
+ out = img[y:y2, x:x2]
217
+ p = write_image(output_path, out)
218
+ return f"Cropped to {out.shape[1]}x{out.shape[0]}, saved {p}"
219
+
220
+
221
+ @mcp.tool()
222
+ def image_rotate(input_path: str, output_path: str,
223
+ angle: float, scale: float = 1.0,
224
+ keep_size: bool = False) -> str:
225
+ """Rotate an image by `angle` degrees (CCW). If keep_size is False the
226
+ output canvas is expanded to fit the rotated image."""
227
+ img = read_image(input_path)
228
+ h, w = img.shape[:2]
229
+ cx, cy = w / 2.0, h / 2.0
230
+ M = cv2.getRotationMatrix2D((cx, cy), float(angle), float(scale))
231
+ if keep_size:
232
+ nw, nh = w, h
233
+ else:
234
+ cos, sin = abs(M[0, 0]), abs(M[0, 1])
235
+ nw = int(h * sin + w * cos)
236
+ nh = int(h * cos + w * sin)
237
+ M[0, 2] += nw / 2 - cx
238
+ M[1, 2] += nh / 2 - cy
239
+ out = cv2.warpAffine(img, M, (nw, nh))
240
+ p = write_image(output_path, out)
241
+ return f"Rotated by {angle}deg, saved {p}"
242
+
243
+
244
+ @mcp.tool()
245
+ def image_flip(input_path: str, output_path: str, direction: str = "horizontal") -> str:
246
+ """Flip image: horizontal (mirror), vertical, or both."""
247
+ img = read_image(input_path)
248
+ code = {"horizontal": 1, "vertical": 0, "both": -1}.get(direction)
249
+ if code is None:
250
+ raise ValueError("direction must be horizontal, vertical, or both")
251
+ p = write_image(output_path, cv2.flip(img, code))
252
+ return f"Flipped ({direction}), saved {p}"
253
+
254
+
255
+ @mcp.tool()
256
+ def image_pad(input_path: str, output_path: str,
257
+ top: int = 0, bottom: int = 0, left: int = 0, right: int = 0,
258
+ border_type: str = "constant", color: str = "0,0,0") -> str:
259
+ """Add borders/padding around an image.
260
+ border_type: constant, replicate, reflect, reflect101, wrap."""
261
+ img = read_image(input_path)
262
+ bmap = {"constant": cv2.BORDER_CONSTANT, "replicate": cv2.BORDER_REPLICATE,
263
+ "reflect": cv2.BORDER_REFLECT, "reflect101": cv2.BORDER_REFLECT101,
264
+ "wrap": cv2.BORDER_WRAP}
265
+ bgr = parse_color(color)
266
+ out = cv2.copyMakeBorder(img, top, bottom, left, right,
267
+ bmap.get(border_type, cv2.BORDER_CONSTANT), value=bgr)
268
+ p = write_image(output_path, out)
269
+ return f"Padded image, saved {p}"
270
+
271
+
272
+ # ============================================================================
273
+ # 4. COLOR
274
+ # ============================================================================
275
+
276
+ @mcp.tool()
277
+ def image_to_grayscale(input_path: str, output_path: str) -> str:
278
+ """Convert image to single-channel grayscale."""
279
+ img = read_image(input_path, cv2.IMREAD_COLOR)
280
+ p = write_image(output_path, cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
281
+ return f"Grayscale image saved to {p}"
282
+
283
+
284
+ @mcp.tool()
285
+ def color_convert(input_path: str, output_path: str, target: str) -> str:
286
+ """Convert color space. target: gray, hsv, hls, lab, ycrcb, rgb, bgr."""
287
+ img = read_image(input_path, cv2.IMREAD_COLOR)
288
+ cmap = {
289
+ "gray": cv2.COLOR_BGR2GRAY, "hsv": cv2.COLOR_BGR2HSV,
290
+ "hls": cv2.COLOR_BGR2HLS, "lab": cv2.COLOR_BGR2LAB,
291
+ "ycrcb": cv2.COLOR_BGR2YCrCb, "rgb": cv2.COLOR_BGR2RGB,
292
+ "bgr": None,
293
+ }
294
+ if target not in cmap:
295
+ raise ValueError(f"Unknown target {target}")
296
+ out = img if cmap[target] is None else cv2.cvtColor(img, cmap[target])
297
+ p = write_image(output_path, out)
298
+ return f"Converted to {target}, saved {p}"
299
+
300
+
301
+ @mcp.tool()
302
+ def adjust_brightness_contrast(input_path: str, output_path: str,
303
+ brightness: int = 0, contrast: float = 1.0) -> str:
304
+ """Adjust brightness (-255..255) and contrast (multiplier, e.g. 1.2)."""
305
+ img = read_image(input_path)
306
+ out = cv2.convertScaleAbs(img, alpha=float(contrast), beta=float(brightness))
307
+ p = write_image(output_path, out)
308
+ return f"Adjusted brightness/contrast, saved {p}"
309
+
310
+
311
+ @mcp.tool()
312
+ def histogram_equalize(input_path: str, output_path: str, method: str = "clahe") -> str:
313
+ """Equalize histogram. method: 'global' (single-channel only) or
314
+ 'clahe' (adaptive, works on color via LAB L-channel)."""
315
+ img = read_image(input_path)
316
+ if method == "global":
317
+ if img.ndim == 3:
318
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
319
+ out = cv2.equalizeHist(img)
320
+ else:
321
+ clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
322
+ if img.ndim == 2:
323
+ out = clahe.apply(img)
324
+ else:
325
+ lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
326
+ lab[..., 0] = clahe.apply(lab[..., 0])
327
+ out = cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
328
+ p = write_image(output_path, out)
329
+ return f"Histogram equalized ({method}), saved {p}"
330
+
331
+
332
+ @mcp.tool()
333
+ def histogram_data(input_path: str, bins: int = 32) -> dict:
334
+ """Return per-channel histograms as lists (B, G, R or single 'gray')."""
335
+ img = read_image(input_path)
336
+ bins = int(max(2, min(bins, 256)))
337
+ out: dict = {"bins": bins, "range": [0, 256]}
338
+ if img.ndim == 2:
339
+ h = cv2.calcHist([img], [0], None, [bins], [0, 256]).flatten().tolist()
340
+ out["gray"] = h
341
+ else:
342
+ for i, name in enumerate(["b", "g", "r"]):
343
+ h = cv2.calcHist([img], [i], None, [bins], [0, 256]).flatten().tolist()
344
+ out[name] = h
345
+ return out
346
+
347
+
348
+ # ============================================================================
349
+ # 5. FILTERING / BLUR / SHARPEN
350
+ # ============================================================================
351
+
352
+ @mcp.tool()
353
+ def blur_gaussian(input_path: str, output_path: str, ksize: int = 5, sigma: float = 0.0) -> str:
354
+ """Apply Gaussian blur. ksize is forced to odd."""
355
+ img = read_image(input_path)
356
+ k = odd(ksize, 1)
357
+ p = write_image(output_path, cv2.GaussianBlur(img, (k, k), float(sigma)))
358
+ return f"Gaussian blur k={k} sigma={sigma}, saved {p}"
359
+
360
+
361
+ @mcp.tool()
362
+ def blur_median(input_path: str, output_path: str, ksize: int = 5) -> str:
363
+ """Median blur — good for salt-and-pepper noise."""
364
+ img = read_image(input_path)
365
+ k = odd(ksize, 3)
366
+ p = write_image(output_path, cv2.medianBlur(img, k))
367
+ return f"Median blur k={k}, saved {p}"
368
+
369
+
370
+ @mcp.tool()
371
+ def blur_bilateral(input_path: str, output_path: str,
372
+ d: int = 9, sigma_color: float = 75, sigma_space: float = 75) -> str:
373
+ """Edge-preserving bilateral filter."""
374
+ img = read_image(input_path)
375
+ p = write_image(output_path,
376
+ cv2.bilateralFilter(img, int(d), float(sigma_color), float(sigma_space)))
377
+ return f"Bilateral filter, saved {p}"
378
+
379
+
380
+ @mcp.tool()
381
+ def sharpen(input_path: str, output_path: str, amount: float = 1.0) -> str:
382
+ """Unsharp-mask sharpening. `amount` ~ 0.5–2.0."""
383
+ img = read_image(input_path)
384
+ blur = cv2.GaussianBlur(img, (0, 0), 3)
385
+ out = cv2.addWeighted(img, 1 + float(amount), blur, -float(amount), 0)
386
+ p = write_image(output_path, out)
387
+ return f"Sharpened (amount={amount}), saved {p}"
388
+
389
+
390
+ @mcp.tool()
391
+ def denoise(input_path: str, output_path: str, strength: int = 10) -> str:
392
+ """Non-local means denoising for color images."""
393
+ img = read_image(input_path, cv2.IMREAD_COLOR)
394
+ out = cv2.fastNlMeansDenoisingColored(img, None, float(strength), float(strength), 7, 21)
395
+ p = write_image(output_path, out)
396
+ return f"Denoised (strength={strength}), saved {p}"
397
+
398
+
399
+ # ============================================================================
400
+ # 6. EDGES / GRADIENTS
401
+ # ============================================================================
402
+
403
+ @mcp.tool()
404
+ def edges_canny(input_path: str, output_path: str,
405
+ threshold1: int = 100, threshold2: int = 200) -> str:
406
+ """Canny edge detector."""
407
+ img = read_image(input_path, cv2.IMREAD_GRAYSCALE)
408
+ p = write_image(output_path, cv2.Canny(img, int(threshold1), int(threshold2)))
409
+ return f"Canny edges saved to {p}"
410
+
411
+
412
+ @mcp.tool()
413
+ def edges_sobel(input_path: str, output_path: str, ksize: int = 3) -> str:
414
+ """Sobel gradient magnitude."""
415
+ img = read_image(input_path, cv2.IMREAD_GRAYSCALE)
416
+ k = odd(ksize, 1)
417
+ gx = cv2.Sobel(img, cv2.CV_32F, 1, 0, ksize=k)
418
+ gy = cv2.Sobel(img, cv2.CV_32F, 0, 1, ksize=k)
419
+ mag = cv2.magnitude(gx, gy)
420
+ out = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
421
+ p = write_image(output_path, out)
422
+ return f"Sobel magnitude saved to {p}"
423
+
424
+
425
+ @mcp.tool()
426
+ def edges_laplacian(input_path: str, output_path: str, ksize: int = 3) -> str:
427
+ """Laplacian of an image."""
428
+ img = read_image(input_path, cv2.IMREAD_GRAYSCALE)
429
+ out = cv2.convertScaleAbs(cv2.Laplacian(img, cv2.CV_16S, ksize=odd(ksize, 1)))
430
+ p = write_image(output_path, out)
431
+ return f"Laplacian saved to {p}"
432
+
433
+
434
+ # ============================================================================
435
+ # 7. THRESHOLDING / MORPHOLOGY
436
+ # ============================================================================
437
+
438
+ @mcp.tool()
439
+ def threshold(input_path: str, output_path: str,
440
+ method: str = "otsu", thresh: int = 127, maxval: int = 255) -> str:
441
+ """Binary threshold. method: binary, binary_inv, otsu, adaptive_mean, adaptive_gaussian."""
442
+ img = read_image(input_path, cv2.IMREAD_GRAYSCALE)
443
+ if method == "binary":
444
+ _, out = cv2.threshold(img, thresh, maxval, cv2.THRESH_BINARY)
445
+ elif method == "binary_inv":
446
+ _, out = cv2.threshold(img, thresh, maxval, cv2.THRESH_BINARY_INV)
447
+ elif method == "otsu":
448
+ _, out = cv2.threshold(img, 0, maxval, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
449
+ elif method == "adaptive_mean":
450
+ out = cv2.adaptiveThreshold(img, maxval, cv2.ADAPTIVE_THRESH_MEAN_C,
451
+ cv2.THRESH_BINARY, 11, 2)
452
+ elif method == "adaptive_gaussian":
453
+ out = cv2.adaptiveThreshold(img, maxval, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
454
+ cv2.THRESH_BINARY, 11, 2)
455
+ else:
456
+ raise ValueError(f"Unknown method {method}")
457
+ p = write_image(output_path, out)
458
+ return f"Thresholded ({method}), saved {p}"
459
+
460
+
461
+ @mcp.tool()
462
+ def morphology(input_path: str, output_path: str, op: str = "open",
463
+ ksize: int = 3, iterations: int = 1) -> str:
464
+ """Morphological op: erode, dilate, open, close, gradient, tophat, blackhat."""
465
+ img = read_image(input_path)
466
+ k = odd(ksize, 1)
467
+ kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (k, k))
468
+ omap = {"erode": cv2.MORPH_ERODE, "dilate": cv2.MORPH_DILATE,
469
+ "open": cv2.MORPH_OPEN, "close": cv2.MORPH_CLOSE,
470
+ "gradient": cv2.MORPH_GRADIENT, "tophat": cv2.MORPH_TOPHAT,
471
+ "blackhat": cv2.MORPH_BLACKHAT}
472
+ if op not in omap:
473
+ raise ValueError(f"Unknown op {op}")
474
+ out = cv2.morphologyEx(img, omap[op], kernel, iterations=int(iterations))
475
+ p = write_image(output_path, out)
476
+ return f"Morphology {op} k={k}, saved {p}"
477
+
478
+
479
+ # ============================================================================
480
+ # 8. CONTOURS / SHAPES
481
+ # ============================================================================
482
+
483
+ @mcp.tool()
484
+ def find_contours(input_path: str, output_path: str = "",
485
+ thresh: int = 127, min_area: float = 50.0) -> dict:
486
+ """Find contours after thresholding. Optionally draw them onto output_path.
487
+ Returns count and bounding boxes."""
488
+ src = read_image(input_path)
489
+ gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) if src.ndim == 3 else src
490
+ _, bw = cv2.threshold(gray, int(thresh), 255, cv2.THRESH_BINARY)
491
+ contours, _ = cv2.findContours(bw, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
492
+ keep = [c for c in contours if cv2.contourArea(c) >= min_area]
493
+ boxes = []
494
+ for c in keep:
495
+ x, y, w, h = cv2.boundingRect(c)
496
+ boxes.append({"x": int(x), "y": int(y), "w": int(w), "h": int(h),
497
+ "area": float(cv2.contourArea(c))})
498
+ if output_path:
499
+ vis = ensure_bgr(src).copy()
500
+ cv2.drawContours(vis, keep, -1, (0, 255, 0), 2)
501
+ write_image(output_path, vis)
502
+ return {"count": len(keep), "boxes": boxes,
503
+ "annotated_image": output_path or None}
504
+
505
+
506
+ @mcp.tool()
507
+ def detect_circles(input_path: str, output_path: str = "",
508
+ dp: float = 1.2, min_dist: float = 30,
509
+ param1: float = 100, param2: float = 30,
510
+ min_radius: int = 0, max_radius: int = 0) -> dict:
511
+ """Hough circle detection."""
512
+ img = read_image(input_path)
513
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if img.ndim == 3 else img
514
+ gray = cv2.medianBlur(gray, 5)
515
+ circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, dp=float(dp),
516
+ minDist=float(min_dist), param1=float(param1),
517
+ param2=float(param2), minRadius=int(min_radius),
518
+ maxRadius=int(max_radius))
519
+ out_list = []
520
+ if circles is not None:
521
+ for x, y, r in np.round(circles[0]).astype(int):
522
+ out_list.append({"x": int(x), "y": int(y), "r": int(r)})
523
+ if output_path:
524
+ vis = ensure_bgr(img).copy()
525
+ for c in out_list:
526
+ cv2.circle(vis, (c["x"], c["y"]), c["r"], (0, 255, 0), 2)
527
+ cv2.circle(vis, (c["x"], c["y"]), 2, (0, 0, 255), 3)
528
+ write_image(output_path, vis)
529
+ return {"count": len(out_list), "circles": out_list,
530
+ "annotated_image": output_path or None}
531
+
532
+
533
+ @mcp.tool()
534
+ def detect_lines(input_path: str, output_path: str = "",
535
+ canny1: int = 50, canny2: int = 150,
536
+ threshold: int = 80, min_line_length: int = 50,
537
+ max_line_gap: int = 10) -> dict:
538
+ """Probabilistic Hough line detection."""
539
+ img = read_image(input_path)
540
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if img.ndim == 3 else img
541
+ edges = cv2.Canny(gray, canny1, canny2)
542
+ lines = cv2.HoughLinesP(edges, 1, np.pi / 180, int(threshold),
543
+ minLineLength=int(min_line_length),
544
+ maxLineGap=int(max_line_gap))
545
+ out_list = []
546
+ if lines is not None:
547
+ for x1, y1, x2, y2 in lines.reshape(-1, 4):
548
+ out_list.append({"x1": int(x1), "y1": int(y1),
549
+ "x2": int(x2), "y2": int(y2)})
550
+ if output_path:
551
+ vis = ensure_bgr(img).copy()
552
+ for L in out_list:
553
+ cv2.line(vis, (L["x1"], L["y1"]), (L["x2"], L["y2"]), (0, 255, 0), 2)
554
+ write_image(output_path, vis)
555
+ return {"count": len(out_list), "lines": out_list,
556
+ "annotated_image": output_path or None}
557
+
558
+
559
+ @mcp.tool()
560
+ def detect_corners(input_path: str, output_path: str = "",
561
+ max_corners: int = 100, quality: float = 0.01,
562
+ min_distance: float = 10) -> dict:
563
+ """Shi-Tomasi good-features-to-track corner detection."""
564
+ img = read_image(input_path)
565
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if img.ndim == 3 else img
566
+ pts = cv2.goodFeaturesToTrack(gray, int(max_corners), float(quality), float(min_distance))
567
+ out_list = []
568
+ if pts is not None:
569
+ for x, y in pts.reshape(-1, 2):
570
+ out_list.append({"x": float(x), "y": float(y)})
571
+ if output_path:
572
+ vis = ensure_bgr(img).copy()
573
+ for c in out_list:
574
+ cv2.circle(vis, (int(c["x"]), int(c["y"])), 4, (0, 255, 0), -1)
575
+ write_image(output_path, vis)
576
+ return {"count": len(out_list), "corners": out_list,
577
+ "annotated_image": output_path or None}
578
+
579
+
580
+ # ============================================================================
581
+ # 9. FEATURE MATCHING (ORB)
582
+ # ============================================================================
583
+
584
+ @mcp.tool()
585
+ def feature_match(image1: str, image2: str, output_path: str = "",
586
+ max_features: int = 500, top_matches: int = 50) -> dict:
587
+ """Detect and match ORB features between two images. Optionally save a
588
+ side-by-side visualization."""
589
+ a = read_image(image1, cv2.IMREAD_GRAYSCALE)
590
+ b = read_image(image2, cv2.IMREAD_GRAYSCALE)
591
+ orb = cv2.ORB_create(nfeatures=int(max_features))
592
+ ka, da = orb.detectAndCompute(a, None)
593
+ kb, db = orb.detectAndCompute(b, None)
594
+ if da is None or db is None:
595
+ return {"keypoints_a": 0, "keypoints_b": 0, "matches": 0}
596
+ bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
597
+ matches = sorted(bf.match(da, db), key=lambda m: m.distance)[: int(top_matches)]
598
+ if output_path:
599
+ vis = cv2.drawMatches(a, ka, b, kb, matches, None,
600
+ flags=cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
601
+ write_image(output_path, vis)
602
+ return {
603
+ "keypoints_a": len(ka), "keypoints_b": len(kb),
604
+ "matches": len(matches),
605
+ "mean_distance": float(np.mean([m.distance for m in matches])) if matches else 0.0,
606
+ "annotated_image": output_path or None,
607
+ }
608
+
609
+
610
+ # ============================================================================
611
+ # 10. OBJECT DETECTION (Haar cascades, bundled with OpenCV)
612
+ # ============================================================================
613
+
614
+ def _cascade(name: str) -> cv2.CascadeClassifier:
615
+ path = Path(cv2.data.haarcascades) / name
616
+ cc = cv2.CascadeClassifier(str(path))
617
+ if cc.empty():
618
+ raise RuntimeError(f"Could not load cascade: {path}")
619
+ return cc
620
+
621
+
622
+ @mcp.tool()
623
+ def detect_faces(input_path: str, output_path: str = "",
624
+ scale_factor: float = 1.1, min_neighbors: int = 5) -> dict:
625
+ """Detect faces with the bundled Haar cascade. Returns bounding boxes."""
626
+ img = read_image(input_path)
627
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if img.ndim == 3 else img
628
+ cc = _cascade("haarcascade_frontalface_default.xml")
629
+ faces = cc.detectMultiScale(gray, scaleFactor=float(scale_factor),
630
+ minNeighbors=int(min_neighbors))
631
+ boxes = [{"x": int(x), "y": int(y), "w": int(w), "h": int(h)} for (x, y, w, h) in faces]
632
+ if output_path:
633
+ vis = ensure_bgr(img).copy()
634
+ for b in boxes:
635
+ cv2.rectangle(vis, (b["x"], b["y"]),
636
+ (b["x"] + b["w"], b["y"] + b["h"]), (0, 255, 0), 2)
637
+ write_image(output_path, vis)
638
+ return {"count": len(boxes), "faces": boxes, "annotated_image": output_path or None}
639
+
640
+
641
+ @mcp.tool()
642
+ def detect_eyes(input_path: str, output_path: str = "") -> dict:
643
+ """Detect eyes with the bundled Haar cascade."""
644
+ img = read_image(input_path)
645
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if img.ndim == 3 else img
646
+ cc = _cascade("haarcascade_eye.xml")
647
+ eyes = cc.detectMultiScale(gray)
648
+ boxes = [{"x": int(x), "y": int(y), "w": int(w), "h": int(h)} for (x, y, w, h) in eyes]
649
+ if output_path:
650
+ vis = ensure_bgr(img).copy()
651
+ for b in boxes:
652
+ cv2.rectangle(vis, (b["x"], b["y"]),
653
+ (b["x"] + b["w"], b["y"] + b["h"]), (255, 0, 0), 2)
654
+ write_image(output_path, vis)
655
+ return {"count": len(boxes), "eyes": boxes, "annotated_image": output_path or None}
656
+
657
+
658
+ @mcp.tool()
659
+ def detect_bodies(input_path: str, output_path: str = "") -> dict:
660
+ """Detect full bodies with the bundled Haar cascade (low recall, demo only)."""
661
+ img = read_image(input_path)
662
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if img.ndim == 3 else img
663
+ cc = _cascade("haarcascade_fullbody.xml")
664
+ boxes_raw = cc.detectMultiScale(gray)
665
+ boxes = [{"x": int(x), "y": int(y), "w": int(w), "h": int(h)} for (x, y, w, h) in boxes_raw]
666
+ if output_path:
667
+ vis = ensure_bgr(img).copy()
668
+ for b in boxes:
669
+ cv2.rectangle(vis, (b["x"], b["y"]),
670
+ (b["x"] + b["w"], b["y"] + b["h"]), (0, 0, 255), 2)
671
+ write_image(output_path, vis)
672
+ return {"count": len(boxes), "bodies": boxes, "annotated_image": output_path or None}
673
+
674
+
675
+ @mcp.tool()
676
+ def detect_qrcode(input_path: str) -> dict:
677
+ """Detect and decode QR codes."""
678
+ img = read_image(input_path)
679
+ det = cv2.QRCodeDetector()
680
+ data, points, _ = det.detectAndDecode(img)
681
+ return {
682
+ "data": data,
683
+ "found": bool(data) or points is not None,
684
+ "points": points.tolist() if points is not None else None,
685
+ }
686
+
687
+
688
+ # ============================================================================
689
+ # 11. DRAWING / ANNOTATION
690
+ # ============================================================================
691
+
692
+ @mcp.tool()
693
+ def draw_rectangle(input_path: str, output_path: str,
694
+ x: int, y: int, width: int, height: int,
695
+ color: str = "0,255,0", thickness: int = 2) -> str:
696
+ """Draw a rectangle on an image."""
697
+ img = ensure_bgr(read_image(input_path)).copy()
698
+ cv2.rectangle(img, (x, y), (x + width, y + height), parse_color(color), int(thickness))
699
+ p = write_image(output_path, img)
700
+ return f"Saved {p}"
701
+
702
+
703
+ @mcp.tool()
704
+ def draw_circle(input_path: str, output_path: str,
705
+ x: int, y: int, radius: int,
706
+ color: str = "0,255,0", thickness: int = 2) -> str:
707
+ """Draw a circle. thickness=-1 fills."""
708
+ img = ensure_bgr(read_image(input_path)).copy()
709
+ cv2.circle(img, (x, y), int(radius), parse_color(color), int(thickness))
710
+ p = write_image(output_path, img)
711
+ return f"Saved {p}"
712
+
713
+
714
+ @mcp.tool()
715
+ def draw_line(input_path: str, output_path: str,
716
+ x1: int, y1: int, x2: int, y2: int,
717
+ color: str = "0,255,0", thickness: int = 2) -> str:
718
+ """Draw a line."""
719
+ img = ensure_bgr(read_image(input_path)).copy()
720
+ cv2.line(img, (x1, y1), (x2, y2), parse_color(color), int(thickness))
721
+ p = write_image(output_path, img)
722
+ return f"Saved {p}"
723
+
724
+
725
+ @mcp.tool()
726
+ def draw_text(input_path: str, output_path: str,
727
+ text: str, x: int, y: int,
728
+ color: str = "255,255,255", scale: float = 1.0, thickness: int = 2) -> str:
729
+ """Draw text on an image."""
730
+ img = ensure_bgr(read_image(input_path)).copy()
731
+ cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_SIMPLEX,
732
+ float(scale), parse_color(color), int(thickness), cv2.LINE_AA)
733
+ p = write_image(output_path, img)
734
+ return f"Saved {p}"
735
+
736
+
737
+ # ============================================================================
738
+ # 12. COMPOSITION / ARITHMETIC
739
+ # ============================================================================
740
+
741
+ @mcp.tool()
742
+ def image_blend(image1: str, image2: str, output_path: str, alpha: float = 0.5) -> str:
743
+ """Blend two same-size images: out = a*img1 + (1-a)*img2."""
744
+ a = read_image(image1)
745
+ b = read_image(image2)
746
+ if a.shape != b.shape:
747
+ b = cv2.resize(b, (a.shape[1], a.shape[0]))
748
+ out = cv2.addWeighted(a, float(alpha), b, 1 - float(alpha), 0)
749
+ p = write_image(output_path, out)
750
+ return f"Blended (alpha={alpha}), saved {p}"
751
+
752
+
753
+ @mcp.tool()
754
+ def image_diff(image1: str, image2: str, output_path: str = "") -> dict:
755
+ """Absolute difference between two images. Returns mean/max diff."""
756
+ a = read_image(image1)
757
+ b = read_image(image2)
758
+ if a.shape != b.shape:
759
+ b = cv2.resize(b, (a.shape[1], a.shape[0]))
760
+ diff = cv2.absdiff(a, b)
761
+ if output_path:
762
+ write_image(output_path, diff)
763
+ return {
764
+ "mean_difference": float(diff.mean()),
765
+ "max_difference": int(diff.max()),
766
+ "annotated_image": output_path or None,
767
+ }
768
+
769
+
770
+ @mcp.tool()
771
+ def image_concat(images: List[str], output_path: str, direction: str = "horizontal") -> str:
772
+ """Concatenate images horizontally or vertically (resized to common dim)."""
773
+ if not images:
774
+ raise ValueError("Need at least one image.")
775
+ arrs = [read_image(p) for p in images]
776
+ arrs = [ensure_bgr(a) for a in arrs]
777
+ if direction == "horizontal":
778
+ h = min(a.shape[0] for a in arrs)
779
+ arrs = [cv2.resize(a, (int(a.shape[1] * h / a.shape[0]), h)) for a in arrs]
780
+ out = np.hstack(arrs)
781
+ elif direction == "vertical":
782
+ w = min(a.shape[1] for a in arrs)
783
+ arrs = [cv2.resize(a, (w, int(a.shape[0] * w / a.shape[1]))) for a in arrs]
784
+ out = np.vstack(arrs)
785
+ else:
786
+ raise ValueError("direction must be horizontal or vertical")
787
+ p = write_image(output_path, out)
788
+ return f"Concatenated {len(images)} images, saved {p}"
789
+
790
+
791
+ @mcp.tool()
792
+ def template_match(image_path: str, template_path: str,
793
+ output_path: str = "", threshold: float = 0.8) -> dict:
794
+ """Find a template inside an image (TM_CCOEFF_NORMED). Returns matches above threshold."""
795
+ img = read_image(image_path)
796
+ tpl = read_image(template_path)
797
+ g = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if img.ndim == 3 else img
798
+ t = cv2.cvtColor(tpl, cv2.COLOR_BGR2GRAY) if tpl.ndim == 3 else tpl
799
+ res = cv2.matchTemplate(g, t, cv2.TM_CCOEFF_NORMED)
800
+ ys, xs = np.where(res >= float(threshold))
801
+ th, tw = t.shape[:2]
802
+ matches = [{"x": int(x), "y": int(y), "score": float(res[y, x])}
803
+ for y, x in zip(ys, xs)]
804
+ matches.sort(key=lambda m: -m["score"])
805
+ if output_path:
806
+ vis = ensure_bgr(img).copy()
807
+ for m in matches[:50]:
808
+ cv2.rectangle(vis, (m["x"], m["y"]),
809
+ (m["x"] + tw, m["y"] + th), (0, 255, 0), 2)
810
+ write_image(output_path, vis)
811
+ return {"count": len(matches), "template_size": [int(tw), int(th)],
812
+ "top_matches": matches[:20], "annotated_image": output_path or None}
813
+
814
+
815
+ # ============================================================================
816
+ # 13. VIDEO PROCESSING
817
+ # ============================================================================
818
+
819
+ @mcp.tool()
820
+ def video_info(path: str) -> dict:
821
+ """Return video metadata (fps, width, height, frame count, duration)."""
822
+ p = Path(path).expanduser()
823
+ if not p.exists():
824
+ raise FileNotFoundError(p)
825
+ cap = cv2.VideoCapture(str(p))
826
+ if not cap.isOpened():
827
+ raise RuntimeError(f"Could not open video {p}")
828
+ try:
829
+ fps = cap.get(cv2.CAP_PROP_FPS)
830
+ n = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
831
+ w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
832
+ h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
833
+ return {"path": str(p), "fps": float(fps), "frame_count": n,
834
+ "width": w, "height": h,
835
+ "duration_seconds": float(n / fps) if fps > 0 else None}
836
+ finally:
837
+ cap.release()
838
+
839
+
840
+ @mcp.tool()
841
+ def video_extract_frames(video_path: str, output_dir: str,
842
+ every_n: int = 30, max_frames: int = 100,
843
+ ext: str = "jpg") -> dict:
844
+ """Sample frames from a video and save them. Returns saved paths."""
845
+ cap = cv2.VideoCapture(str(Path(video_path).expanduser()))
846
+ if not cap.isOpened():
847
+ raise RuntimeError(f"Could not open video {video_path}")
848
+ out = Path(output_dir).expanduser()
849
+ out.mkdir(parents=True, exist_ok=True)
850
+ saved = []
851
+ i = 0
852
+ try:
853
+ while len(saved) < max_frames:
854
+ ok, frame = cap.read()
855
+ if not ok:
856
+ break
857
+ if i % max(1, int(every_n)) == 0:
858
+ p = out / f"frame_{i:06d}.{ext}"
859
+ cv2.imwrite(str(p), frame)
860
+ saved.append(str(p))
861
+ i += 1
862
+ finally:
863
+ cap.release()
864
+ return {"saved_count": len(saved), "frames": saved}
865
+
866
+
867
+ @mcp.tool()
868
+ def video_thumbnail(video_path: str, output_path: str, time_seconds: float = 1.0) -> str:
869
+ """Save a single frame from a video at a given time as a thumbnail."""
870
+ cap = cv2.VideoCapture(str(Path(video_path).expanduser()))
871
+ if not cap.isOpened():
872
+ raise RuntimeError(f"Could not open video {video_path}")
873
+ try:
874
+ cap.set(cv2.CAP_PROP_POS_MSEC, float(time_seconds) * 1000.0)
875
+ ok, frame = cap.read()
876
+ if not ok:
877
+ raise RuntimeError("Could not read frame at requested time.")
878
+ p = write_image(output_path, frame)
879
+ return f"Thumbnail saved to {p}"
880
+ finally:
881
+ cap.release()
882
+
883
+
884
+ # ============================================================================
885
+ # Entry point
886
+ # ============================================================================
887
+
888
+ def _log(msg: str) -> None:
889
+ print(msg, file=sys.stderr, flush=True)
890
+
891
+
892
+ def main() -> None:
893
+ """Console-script entry point: run the image-processing MCP server over stdio."""
894
+ _log("farshid-mcp-imageProcessing server starting (stdio)")
895
+ mcp.run()
896
+
897
+
898
+ if __name__ == "__main__":
899
+ main()
@@ -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,8 @@
1
+ farshid_mcp_imageprocessing/__init__.py,sha256=dCR1t1bjyiak9-Ie4WjusQHQ2l3k0EPgLWWvOTCDXaY,112
2
+ farshid_mcp_imageprocessing/cv_helpers.py,sha256=RAvUMqR1avsYAX0N8sczpGgXD_WAX3qY-M-l-M9t6A4,3292
3
+ farshid_mcp_imageprocessing/server.py,sha256=RtS27UBKDUr9F1e3EBsSthUTZdtw25xxqv5qKsad78U,36460
4
+ farshid_mcp_imageprocessing-0.2.1.dist-info/METADATA,sha256=-K_ruhc2hpHGG9b5Nk4xImZeLlPuIgF5CnH19Gtg5no,6982
5
+ farshid_mcp_imageprocessing-0.2.1.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
6
+ farshid_mcp_imageprocessing-0.2.1.dist-info/entry_points.txt,sha256=zremW81Q_9X39tnak5XQmGEQVf2DmdxwPzZf9sHOGr8,88
7
+ farshid_mcp_imageprocessing-0.2.1.dist-info/licenses/LICENSE,sha256=rSQU8rO5M10DDKXH5u80g9fniYfO8sU-8RaGNpQYVTc,1097
8
+ farshid_mcp_imageprocessing-0.2.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.29.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [console_scripts]
2
+ farshid-mcp-imageprocessing = farshid_mcp_imageprocessing.server:main
@@ -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.