Visual tools¶
The 23 visual tools that operate on the active image: geometry, intensity and contrast, edge and morphology, read-only measurement, and annotation. For descriptions of each tool and its effects, see Tools.
visual ¶
Visual tools for radiology image analysis.
Provides zoom, crop, contrast adjustment, thresholding, flip, and rotate tools for interactive image analysis by VLMs.
This module combines: - Core image operations (zoom, crop, contrast, threshold, flip, rotate) - Tool wrappers for VLM integration - Tool factory (create_visual_tools)
zoom_image ¶
zoom_image(
image: Image,
factor: float,
config: ImageProcessingConfig | None = None,
) -> Image.Image
Return a zoomed version of image by scaling with factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
factor
|
float
|
Zoom factor (must be in configured range) |
required |
config
|
ImageProcessingConfig | None
|
Optional image config. If None, uses global default. |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
Zoomed image |
Raises:
| Type | Description |
|---|---|
ValueError
|
If factor is out of valid range |
Source code in src/gaze/tools/visual.py
crop_image ¶
crop_image(
image: Image,
box: tuple[float, float, float, float],
config: ImageProcessingConfig | None = None,
) -> Image.Image
Crop image using normalized coordinates (x1, y1, x2, y2) in range 0-1.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
box
|
tuple[float, float, float, float]
|
Tuple of (x1, y1, x2, y2) normalized coordinates in range [0, 1] where x2 > x1 and y2 > y1 |
required |
config
|
ImageProcessingConfig | None
|
Optional image config. If None, uses global default. |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
Cropped image |
Raises:
| Type | Description |
|---|---|
ValueError
|
If image is too small to crop, coordinates are out of range, or resulting crop would be too small |
Source code in src/gaze/tools/visual.py
adjust_contrast ¶
adjust_contrast(
image: Image,
factor: float,
config: ImageProcessingConfig | None = None,
) -> Image.Image
Adjust contrast of image by factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
factor
|
float
|
Contrast factor (must be in configured range). 1.0 = no change, >1.0 increases contrast, <1.0 decreases contrast. |
required |
config
|
ImageProcessingConfig | None
|
Optional image config. If None, uses global default. |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
Contrast-adjusted image |
Raises:
| Type | Description |
|---|---|
ValueError
|
If factor is out of valid range |
Source code in src/gaze/tools/visual.py
apply_intensity_threshold ¶
apply_intensity_threshold(
image: Image,
lower: int,
upper: int,
config: ImageProcessingConfig | None = None,
) -> Image.Image
Apply intensity threshold to grayscale image and rescale to 0-255.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
lower
|
int
|
Lower intensity bound (0-254) |
required |
upper
|
int
|
Upper intensity bound (must be > lower, max 255) |
required |
config
|
ImageProcessingConfig | None
|
Optional config for min_threshold_window. Uses global default if None. |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
Thresholded grayscale image with intensities rescaled to 0-255 |
Raises:
| Type | Description |
|---|---|
ValueError
|
If bounds are invalid or window width is below minimum |
Source code in src/gaze/tools/visual.py
flip_horizontal ¶
flip_vertical ¶
rotate_90 ¶
Rotate image by 90 degrees.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input image |
required |
clockwise
|
bool
|
If True, rotate clockwise; if False, rotate counter-clockwise |
True
|
Returns:
| Type | Description |
|---|---|
Image
|
Rotated image |
Source code in src/gaze/tools/visual.py
adjust_brightness ¶
adjust_brightness(
image: Image,
factor: float,
config: ImageProcessingConfig | None = None,
) -> Image.Image
Adjust brightness of image by factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
factor
|
float
|
Brightness factor (must be in configured range). 1.0 = no change, >1.0 increases brightness, <1.0 decreases. |
required |
config
|
ImageProcessingConfig | None
|
Optional image config. If None, uses global default. |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
Brightness-adjusted image |
Raises:
| Type | Description |
|---|---|
ValueError
|
If factor is out of valid range |
Source code in src/gaze/tools/visual.py
adjust_sharpness ¶
adjust_sharpness(
image: Image,
factor: float,
config: ImageProcessingConfig | None = None,
) -> Image.Image
Adjust sharpness of image by factor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
factor
|
float
|
Sharpness factor (must be in configured range). 0.0 = blurred, 1.0 = original, >1.0 = sharpened. |
required |
config
|
ImageProcessingConfig | None
|
Optional image config. If None, uses global default. |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
Sharpness-adjusted image |
Raises:
| Type | Description |
|---|---|
ValueError
|
If factor is out of valid range |
Source code in src/gaze/tools/visual.py
equalize_histogram ¶
Equalize the histogram of image for improved contrast distribution.
Converts to grayscale first since brain MRI is inherently grayscale; per-channel equalization on RGB is meaningless for medical imaging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
Returns:
| Type | Description |
|---|---|
Image
|
Histogram-equalized grayscale image |
Source code in src/gaze/tools/visual.py
get_intensity_stats ¶
get_intensity_stats(
image: Image,
box: tuple[float, float, float, float] | None = None,
) -> dict[str, object]
Compute intensity statistics over image or a sub-region.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
box
|
tuple[float, float, float, float] | None
|
Optional normalized coordinates (x1, y1, x2, y2) in [0, 1] for a sub-region. If None, computes stats over the full image. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
Dict with mean, std, min, max, median, and 10-bin histogram. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If box coordinates are invalid |
Source code in src/gaze/tools/visual.py
measure_distance ¶
measure_distance(
image: Image,
point1: tuple[float, float],
point2: tuple[float, float],
) -> dict[str, object]
Measure Euclidean distance between two points on image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image (used for dimension scaling) |
required |
point1
|
tuple[float, float]
|
Normalized (x, y) coordinates in [0, 1] |
required |
point2
|
tuple[float, float]
|
Normalized (x, y) coordinates in [0, 1] |
required |
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
Dict with distance_pixels, point1_pixels, point2_pixels, image_size. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If coordinates are out of [0, 1] range |
Source code in src/gaze/tools/visual.py
draw_grid_overlay ¶
draw_grid_overlay(
image: Image,
divisions: int,
config: ImageProcessingConfig | None = None,
) -> Image.Image
Draw a labeled grid overlay on image.
Draws a divisions x divisions grid with cell labels (A1, B2, etc.). Green lines and yellow text on black background for readability on MRI. Works on a copy; does not mutate the input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
divisions
|
int
|
Number of grid divisions per axis (rows and columns) |
required |
config
|
ImageProcessingConfig | None
|
Optional image config for max_grid_divisions. If None, uses global default. |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
Image with grid overlay (RGB mode) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If divisions is out of valid range |
Source code in src/gaze/tools/visual.py
detect_edges ¶
Detect edges in image using Sobel or Laplacian operators.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
method
|
str
|
Edge detection method ("sobel" or "laplacian") |
'sobel'
|
Returns:
| Type | Description |
|---|---|
Image
|
Grayscale edge map image |
Raises:
| Type | Description |
|---|---|
ValueError
|
If method is not recognized |
Source code in src/gaze/tools/visual.py
compute_symmetry_diff ¶
Compute left-right symmetry difference map.
Flips the image horizontally and computes the absolute pixel-wise difference, highlighting asymmetric regions (potential pathology).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
Returns:
| Type | Description |
|---|---|
Image
|
Grayscale difference map (bright = asymmetric regions) |
Source code in src/gaze/tools/visual.py
annotate_region ¶
annotate_region(
image: Image,
box: tuple[float, float, float, float],
color: str = "red",
label: str | None = None,
) -> Image.Image
Draw a bounding box annotation on image.
Works on a copy; does not mutate the input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
box
|
tuple[float, float, float, float]
|
Normalized coordinates (x1, y1, x2, y2) in [0, 1] |
required |
color
|
str
|
Box color name (e.g. "red", "green", "yellow") |
'red'
|
label
|
str | None
|
Optional text label drawn above the box |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
Annotated image (RGB) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If box coordinates are invalid |
Source code in src/gaze/tools/visual.py
invert_image ¶
Invert pixel intensities (negative image).
Useful for toggling between standard and inverted display modes common in radiology viewers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
Returns:
| Type | Description |
|---|---|
Image
|
Inverted grayscale image |
Source code in src/gaze/tools/visual.py
apply_window_level ¶
apply_window_level(
image: Image,
center: int | None = None,
width: int | None = None,
preset: str | None = None,
) -> Image.Image
Apply clinical window/level to image.
Either provide center+width or a preset name. MRI presets (8-bit): brain, flair, t2, stroke, posterior_fossa. CT presets (Hounsfield units): ct_brain, ct_subdural, ct_bone, ct_soft_tissue, ct_stroke, ct_posterior_fossa.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
center
|
int | None
|
Window center intensity |
None
|
width
|
int | None
|
Window width |
None
|
preset
|
str | None
|
Clinical preset name (overrides center/width) |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
Windowed grayscale image |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither preset nor center+width provided, or invalid preset |
Source code in src/gaze/tools/visual.py
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 | |
adaptive_equalize ¶
adaptive_equalize(
image: Image,
clip_limit: float = 2.0,
tile_size: int = 8,
config: ImageProcessingConfig | None = None,
) -> Image.Image
Apply Contrast Limited Adaptive Histogram Equalization (CLAHE).
Operates on local tiles for better local contrast than global equalization. Particularly useful for brain MRI white matter lesions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
clip_limit
|
float
|
Histogram clip limit (higher = more contrast) |
2.0
|
tile_size
|
int
|
Tile grid size (image divided into tile_size x tile_size tiles) |
8
|
config
|
ImageProcessingConfig | None
|
Optional config for clip_limit bounds |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
CLAHE-processed grayscale image |
Raises:
| Type | Description |
|---|---|
ValueError
|
If clip_limit or tile_size is out of range |
Source code in src/gaze/tools/visual.py
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 | |
compute_intensity_profile ¶
compute_intensity_profile(
image: Image,
point1: tuple[float, float],
point2: tuple[float, float],
) -> dict[str, object]
Sample pixel intensities along a line between two points.
Uses Bresenham-style sampling for accurate pixel traversal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
point1
|
tuple[float, float]
|
Start point as normalized (x, y) in [0, 1] |
required |
point2
|
tuple[float, float]
|
End point as normalized (x, y) in [0, 1] |
required |
Returns:
| Type | Description |
|---|---|
dict[str, object]
|
Dict with profile (list of intensities), positions, stats, and pixel coords. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If coordinates are out of [0, 1] |
Source code in src/gaze/tools/visual.py
denoise_gaussian ¶
denoise_gaussian(
image: Image,
sigma: float,
config: ImageProcessingConfig | None = None,
) -> Image.Image
Apply Gaussian blur for noise reduction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
sigma
|
float
|
Gaussian kernel standard deviation |
required |
config
|
ImageProcessingConfig | None
|
Optional config for sigma bounds |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
Denoised image |
Raises:
| Type | Description |
|---|---|
ValueError
|
If sigma is out of range |
Source code in src/gaze/tools/visual.py
morphological_op ¶
morphological_op(
image: Image,
operation: str,
iterations: int = 1,
threshold_value: int | None = None,
config: ImageProcessingConfig | None = None,
) -> Image.Image
Apply morphological operation (erode, dilate, open, close).
Uses PIL's MinFilter (erosion) and MaxFilter (dilation). If threshold_value is provided, first binarizes the image at that intensity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Image
|
Input PIL Image |
required |
operation
|
str
|
One of "erode", "dilate", "open", "close" |
required |
iterations
|
int
|
Number of times to apply the operation |
1
|
threshold_value
|
int | None
|
Optional intensity threshold for binarization (0-255) |
None
|
config
|
ImageProcessingConfig | None
|
Optional config for max iterations |
None
|
Returns:
| Type | Description |
|---|---|
Image
|
Processed grayscale image |
Raises:
| Type | Description |
|---|---|
ValueError
|
If operation is invalid or iterations out of range |
Source code in src/gaze/tools/visual.py
create_visual_tools ¶
create_visual_tools(
disabled_tools: set[str] | None = None,
config: ImageProcessingConfig | None = None,
) -> list[Tool]
Create the standard set of visual tools for image analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
disabled_tools
|
set[str] | None
|
Set of tool names to exclude from the returned list |
None
|
config
|
ImageProcessingConfig | None
|
Image processing config for schema ranges. If None, uses global default. |
None
|
Returns:
| Type | Description |
|---|---|
list[Tool]
|
List of Tool objects ready for registration with ToolRegistry |
Source code in src/gaze/tools/visual.py
1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 | |