Medical-research-skills microscopy-scale-bar-adder
Add accurate, publication-ready scale bars to microscopy images given pixel-to-unit calibration data.
git clone https://github.com/aipoch/medical-research-skills
T=$(mktemp -d) && git clone --depth=1 https://github.com/aipoch/medical-research-skills "$T" && mkdir -p ~/.claude/skills && cp -r "$T/scientific-skills/Other/microscopy-scale-bar-adder" ~/.claude/skills/aipoch-medical-research-skills-microscopy-scale-bar-adder && rm -rf "$T"
scientific-skills/Other/microscopy-scale-bar-adder/SKILL.mdMicroscopy 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 |
|---|---|---|---|---|
| path | Yes | - | Input image file path |
| float | Yes | - | Scale bar length in physical units |
| str | No | | Unit: , , |
| float | No | from TIFF metadata | Calibration override (pixels per unit) |
| path | No | | Output file path |
| str | No | | Bar position: , , , |
| str | No | | Scale bar fill color |
| str | No | | Label text color |
| int | No | | 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../
to stderr and exit with code 1.Error: Path traversal detected: {path} - Image open —
. RaisePIL.Image.open(args.image)
if missing.FileNotFoundError - Pixel length calculation — derive pixels-per-unit from image metadata (TIFF XResolution tag) or require user to supply
. Scale bar pixel length =--pixels-per-unit
.scale * pixels_per_unit - Draw scale bar — use
to draw a filled rectangle at the specified position withPIL.ImageDraw.Draw(img)
height.--bar-thickness - Draw label — use
to renderPIL.ImageFont
above or below the bar."{scale} {unit}" - Save output —
. Print the output path to stdout.img.save(output_path)
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
,toprighttopleft - 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
or--image
is missing, state exactly which fields are missing and request only those.--scale - If the image file path contains
or points outside the workspace, reject with:../
and exit with code 1.Error: Path traversal detected: {path} - If the image file does not exist, print
to stderr and exit with code 1.Error: File not found: {path} - If
is not one of the four valid values, reject with a clear error listing valid options.--position - If TIFF XResolution metadata is absent and
is not provided, request the calibration value before proceeding.--pixels-per-unit - If the task goes outside the documented scope, stop instead of guessing or silently widening the assignment.
- If
fails, report the failure point and summarize what can still be completed.scripts/main.py - 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