Source: https://github.com/aipoch/medical-research-skills
Microscopy Scale Bar Adder
Add accurate scale bars to microscopy images for publication-ready figures using Pillow for image processing.
⚠️ POLISHED CANDIDATE — Requires Fresh Evaluation
The original script was a stub that never modified images. This polished version documents the full Pillow-based implementation, adds all missing CLI parameters, and enforces path traversal protection.
When to Use
- Adding scale bars to fluorescence, brightfield, or electron microscopy images
- Preparing microscopy figures for journal submission
- Batch-processing image sets with consistent scale bar styling
- Verifying scale bar accuracy against known calibration data
Workflow
- Confirm the user objective, required inputs, and non-negotiable constraints before doing detailed work.
- Validate that the request matches the documented scope and stop early if the task would require unsupported assumptions.
- Use the packaged script path or the documented reasoning path with only the inputs that are actually available.
- Return a structured result that separates assumptions, deliverables, risks, and unresolved items.
- If execution fails or inputs are incomplete, switch to the fallback path and state exactly what blocked full completion.
Usage
# Add a 50 µm scale bar to a TIFF image
python scripts/main.py --image image.tif --scale 50 --unit um
# Specify output path and bar position
python scripts/main.py --image image.tif --scale 10 --unit um --output annotated.tif --position bottomright
# Custom bar and label colors
python scripts/main.py --image image.tif --scale 100 --unit nm --bar-color white --label-color white --bar-thickness 4
Parameters
| Parameter |
Type |
Required |
Default |
Description |
--image |
path |
Yes |
- |
Input image file path |
--scale |
float |
Yes |
- |
Scale bar length in physical units |
--unit |
str |
No |
um |
Unit: um, nm, mm |
--pixels-per-unit |
float |
No |
from TIFF metadata |
Calibration override (pixels per unit) |
--output |
path |
No |
<input>_scalebar.<ext> |
Output file path |
--position |
str |
No |
bottomright |
Bar position: bottomright, bottomleft, topright, topleft |
--bar-color |
str |
No |
white |
Scale bar fill color |
--label-color |
str |
No |
white |
Label text color |
--bar-thickness |
int |
No |
3 |
Bar height in pixels |
Implementation Notes (for script developer)
The script must implement using PIL.Image, PIL.ImageDraw, PIL.ImageFont:
- Path validation — reject paths containing
../ or absolute paths outside the workspace before opening any file. Print Error: Path traversal detected: {path} to stderr and exit with code 1.
- Image open —
PIL.Image.open(args.image). Raise FileNotFoundError if missing.
- Pixel length calculation — derive pixels-per-unit from image metadata (TIFF XResolution tag) or require user to supply
--pixels-per-unit. Scale bar pixel length = scale * pixels_per_unit.
- Draw scale bar — use
PIL.ImageDraw.Draw(img) to draw a filled rectangle at the specified position with --bar-thickness height.
- Draw label — use
PIL.ImageFont to render "{scale} {unit}" above or below the bar.
- Save output —
img.save(output_path). Print the output path to stdout.
Features
- Automatic scale bar pixel length calculation from calibration metadata or user-supplied
--pixels-per-unit
- Support for common microscopy formats: TIFF, PNG, JPG, BMP
- Configurable bar size, color, and label style (
--bar-color, --label-color, --bar-thickness)
- Configurable position:
bottomright, bottomleft, topright, topleft
- Preserves original image resolution and metadata
- Path traversal protection (rejects
../ paths and absolute paths outside workspace)
Quick Check
python -m py_compile scripts/main.py
python scripts/main.py --help
Input Validation
This skill accepts: microscopy image files (TIFF, PNG, JPG, BMP) with a physical scale value and unit for scale bar annotation.
If the request does not involve adding a scale bar to a microscopy image — for example, asking to segment cells, perform image analysis, or annotate non-microscopy images — do not proceed. Instead respond:
"microscopy-scale-bar-adder is designed to add calibrated scale bars to microscopy images. Your request appears to be outside this scope. Please provide an image file with scale calibration data, or use a more appropriate tool for your task."
Error Handling
- If
--image or --scale is missing, state exactly which fields are missing and request only those.
- If the image file path contains
../ or points outside the workspace, reject with: Error: Path traversal detected: {path} and exit with code 1.
- If the image file does not exist, print
Error: File not found: {path} to stderr and exit with code 1.
- If
--position is not one of the four valid values, reject with a clear error listing valid options.
- If TIFF XResolution metadata is absent and
--pixels-per-unit is not provided, request the calibration value before proceeding.
- If the task goes outside the documented scope, stop instead of guessing or silently widening the assignment.
- If
scripts/main.py fails, report the failure point and summarize what can still be completed.
- Do not fabricate scale values or calibration data.
Fallback Template
When execution fails or inputs are incomplete, respond with this structure:
FALLBACK REPORT
───────────────────────────────────────
Objective : [restate the goal]
Blocked by : [exact missing input or error]
Partial result : [what can be completed without the missing input]
Next step : [minimum action needed to unblock]
───────────────────────────────────────
Response Template
- Objective
- Inputs Received
- Assumptions
- Workflow
- Deliverable
- Risks and Limits
- Next Checks
Prerequisites
Requires Pillow: pip install Pillow
1---2name: microscopy-scale-bar-adder3description: Add accurate, publication-ready scale bars to microscopy images given pixel-to-unit calibration data.4license: MIT5---6> **Source**: [https://github.com/aipoch/medical-research-skills](https://github.com/aipoch/medical-research-skills)
7
8# Microscopy Scale Bar Adder
9
10Add accurate scale bars to microscopy images for publication-ready figures using Pillow for image processing.
11
12> ⚠️ **POLISHED CANDIDATE — Requires Fresh Evaluation**
13> The original script was a stub that never modified images. This polished version documents the full Pillow-based implementation, adds all missing CLI parameters, and enforces path traversal protection.
14
15## When to Use
16
17- Adding scale bars to fluorescence, brightfield, or electron microscopy images
18- Preparing microscopy figures for journal submission
19- Batch-processing image sets with consistent scale bar styling
20- Verifying scale bar accuracy against known calibration data
21
22## Workflow
23
241. Confirm the user objective, required inputs, and non-negotiable constraints before doing detailed work.
252. Validate that the request matches the documented scope and stop early if the task would require unsupported assumptions.
263. Use the packaged script path or the documented reasoning path with only the inputs that are actually available.
274. Return a structured result that separates assumptions, deliverables, risks, and unresolved items.
285. If execution fails or inputs are incomplete, switch to the fallback path and state exactly what blocked full completion.
29
30## Usage
31
32```text
33# Add a 50 µm scale bar to a TIFF image
34python scripts/main.py --image image.tif --scale 50 --unit um
35
36# Specify output path and bar position
37python scripts/main.py --image image.tif --scale 10 --unit um --output annotated.tif --position bottomright
38
39# Custom bar and label colors
40python scripts/main.py --image image.tif --scale 100 --unit nm --bar-color white --label-color white --bar-thickness 4
41```
42
43## Parameters
44
45| Parameter | Type | Required | Default | Description |
46|-----------|------|----------|---------|-------------|
47| `--image` | path | Yes | - | Input image file path |
48| `--scale` | float | Yes | - | Scale bar length in physical units |
49| `--unit` | str | No | `um` | Unit: `um`, `nm`, `mm` |
50| `--pixels-per-unit` | float | No | from TIFF metadata | Calibration override (pixels per unit) |
51| `--output` | path | No | `<input>_scalebar.<ext>` | Output file path |
52| `--position` | str | No | `bottomright` | Bar position: `bottomright`, `bottomleft`, `topright`, `topleft` |
53| `--bar-color` | str | No | `white` | Scale bar fill color |
54| `--label-color` | str | No | `white` | Label text color |
55| `--bar-thickness` | int | No | `3` | Bar height in pixels |
56
57## Implementation Notes (for script developer)
58
59The script must implement using `PIL.Image`, `PIL.ImageDraw`, `PIL.ImageFont`:
60
611. **Path validation** — reject paths containing `../` or absolute paths outside the workspace before opening any file. Print `Error: Path traversal detected: {path}` to stderr and exit with code 1.
622. **Image open** — `PIL.Image.open(args.image)`. Raise `FileNotFoundError` if missing.
633. **Pixel length calculation** — derive pixels-per-unit from image metadata (TIFF XResolution tag) or require user to supply `--pixels-per-unit`. Scale bar pixel length = `scale * pixels_per_unit`.
644. **Draw scale bar** — use `PIL.ImageDraw.Draw(img)` to draw a filled rectangle at the specified position with `--bar-thickness` height.
655. **Draw label** — use `PIL.ImageFont` to render `"{scale} {unit}"` above or below the bar.
666. **Save output** — `img.save(output_path)`. Print the output path to stdout.
67
68## Features
69
70- Automatic scale bar pixel length calculation from calibration metadata or user-supplied `--pixels-per-unit`
71- Support for common microscopy formats: TIFF, PNG, JPG, BMP
72- Configurable bar size, color, and label style (`--bar-color`, `--label-color`, `--bar-thickness`)
73- Configurable position: `bottomright`, `bottomleft`, `topright`, `topleft`
74- Preserves original image resolution and metadata
75- Path traversal protection (rejects `../` paths and absolute paths outside workspace)
76
77## Quick Check
78
79```bash
80python -m py_compile scripts/main.py
81python scripts/main.py --help
82```
83
84## Input Validation
85
86This skill accepts: microscopy image files (TIFF, PNG, JPG, BMP) with a physical scale value and unit for scale bar annotation.
87
88If the request does not involve adding a scale bar to a microscopy image — for example, asking to segment cells, perform image analysis, or annotate non-microscopy images — do not proceed. Instead respond:
89
90> "microscopy-scale-bar-adder is designed to add calibrated scale bars to microscopy images. Your request appears to be outside this scope. Please provide an image file with scale calibration data, or use a more appropriate tool for your task."
91
92## Error Handling
93
94- If `--image` or `--scale` is missing, state exactly which fields are missing and request only those.
95- If the image file path contains `../` or points outside the workspace, reject with: `Error: Path traversal detected: {path}` and exit with code 1.
96- If the image file does not exist, print `Error: File not found: {path}` to stderr and exit with code 1.
97- If `--position` is not one of the four valid values, reject with a clear error listing valid options.
98- If TIFF XResolution metadata is absent and `--pixels-per-unit` is not provided, request the calibration value before proceeding.
99- If the task goes outside the documented scope, stop instead of guessing or silently widening the assignment.
100- If `scripts/main.py` fails, report the failure point and summarize what can still be completed.
101- Do not fabricate scale values or calibration data.
102
103## Fallback Template
104
105When execution fails or inputs are incomplete, respond with this structure:
106
107```
108FALLBACK REPORT
109───────────────────────────────────────
110Objective : [restate the goal]
111Blocked by : [exact missing input or error]
112Partial result : [what can be completed without the missing input]
113Next step : [minimum action needed to unblock]
114───────────────────────────────────────
115```
116
117## Response Template
118
1191. Objective
1202. Inputs Received
1213. Assumptions
1224. Workflow
1235. Deliverable
1246. Risks and Limits
1257. Next Checks
126
127## Prerequisites
128
129Requires Pillow: `pip install Pillow`