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
- 1. 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
CODEBLOCK0
Parameters
| Parameter | Type | Required | Default | Description |
|---|
| INLINECODE0 | path | Yes | - | Input image file path |
| INLINECODE1 |
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:
- 1. 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 INLINECODE36
- 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, INLINECODE43 - Preserves original image resolution and metadata
- Path traversal protection (rejects
../ paths and absolute paths outside workspace)
Quick Check
CODEBLOCK1
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:
CODEBLOCK2
Response Template
- 1. Objective
- Inputs Received
- Assumptions
- Workflow
- Deliverable
- Risks and Limits
- Next Checks
Prerequisites
Requires Pillow: INLINECODE53
显微镜比例尺添加工具
使用Pillow图像处理库,为显微镜图像添加精确的比例尺,生成可直接用于发表的图像。
⚠️ 优化候选版本 — 需重新评估
原始脚本仅为存根,未实际修改图像。此优化版本记录了完整的基于Pillow的实现,添加了所有缺失的CLI参数,并实施了路径遍历保护。
适用场景
- - 为荧光、明场或电子显微镜图像添加比例尺
- 准备用于期刊投稿的显微镜图像
- 批量处理图像集,保持比例尺样式一致
- 根据已知校准数据验证比例尺精度
工作流程
- 1. 在执行详细工作前,确认用户目标、所需输入和不可协商的约束条件。
- 验证请求是否在文档规定的范围内,如果任务需要不支持的假设条件,则提前终止。
- 仅使用实际可用的输入,使用打包脚本路径或文档化的推理路径。
- 返回结构化结果,区分假设、交付物、风险和未解决项。
- 如果执行失败或输入不完整,切换到备用路径并明确说明阻止完整完成的原因。
使用方法
text
为TIFF图像添加50微米比例尺
python scripts/main.py --image image.tif --scale 50 --unit um
指定输出路径和比例尺位置
python scripts/main.py --image image.tif --scale 10 --unit um --output annotated.tif --position bottomright
自定义比例尺和标签颜色
python scripts/main.py --image image.tif --scale 100 --unit nm --bar-color white --label-color white --bar-thickness 4
参数说明
| 参数 | 类型 | 必需 | 默认值 | 描述 |
|---|
| --image | 路径 | 是 | - | 输入图像文件路径 |
| --scale |
浮点数 | 是 | - | 比例尺的物理单位长度 |
| --unit | 字符串 | 否 | um | 单位:um、nm、mm |
| --pixels-per-unit | 浮点数 | 否 | 来自TIFF元数据 | 校准覆盖值(每单位像素数) |
| --output | 路径 | 否 | <输入文件名>_scalebar.<扩展名> | 输出文件路径 |
| --position | 字符串 | 否 | bottomright | 比例尺位置:bottomright、bottomleft、topright、topleft |
| --bar-color | 字符串 | 否 | white | 比例尺填充颜色 |
| --label-color | 字符串 | 否 | white | 标签文字颜色 |
| --bar-thickness | 整数 | 否 | 3 | 比例尺高度(像素) |
实现说明(供脚本开发者参考)
脚本必须使用PIL.Image、PIL.ImageDraw、PIL.ImageFont实现:
- 1. 路径验证 — 在打开任何文件前,拒绝包含../或工作区外绝对路径的路径。向stderr输出Error: Path traversal detected: {path}并以代码1退出。
- 打开图像 — PIL.Image.open(args.image)。如果文件缺失则抛出FileNotFoundError。
- 像素长度计算 — 从图像元数据(TIFF XResolution标签)获取每单位像素数,或要求用户提供--pixels-per-unit。比例尺像素长度 = scale * pixelsperunit。
- 绘制比例尺 — 使用PIL.ImageDraw.Draw(img)在指定位置绘制填充矩形,高度为--bar-thickness。
- 绘制标签 — 使用PIL.ImageFont在比例尺上方或下方渲染{scale} {unit}。
- 保存输出 — img.save(output_path)。将输出路径打印到stdout。
功能特点
- - 根据校准元数据或用户提供的--pixels-per-unit自动计算比例尺像素长度
- 支持常见显微镜格式:TIFF、PNG、JPG、BMP
- 可配置比例尺大小、颜色和标签样式(--bar-color、--label-color、--bar-thickness)
- 可配置位置:bottomright、bottomleft、topright、topleft
- 保留原始图像分辨率和元数据
- 路径遍历保护(拒绝../路径和工作区外的绝对路径)
快速检查
bash
python -m py_compile scripts/main.py
python scripts/main.py --help
输入验证
本技能接受:带有物理比例值和单位的显微镜图像文件(TIFF、PNG、JPG、BMP),用于添加比例尺标注。
如果请求不涉及为显微镜图像添加比例尺——例如要求分割细胞、进行图像分析或标注非显微镜图像——请勿继续。应回复:
microscopy-scale-bar-adder 旨在为显微镜图像添加校准比例尺。您的请求似乎超出了此范围。请提供带有比例校准数据的图像文件,或使用更适合您任务的工具。
错误处理
- - 如果缺少--image或--scale,明确说明缺少哪些字段,并仅请求这些字段。
- 如果图像文件路径包含../或指向工作区外,拒绝并输出:Error: Path traversal detected: {path}并以代码1退出。
- 如果图像文件不存在,向stderr输出Error: File not found: {path}并以代码1退出。
- 如果--position不是四个有效值之一,拒绝并列出有效选项的清晰错误信息。
- 如果TIFF XResolution元数据缺失且未提供--pixels-per-unit,在继续前请求校准值。
- 如果任务超出文档范围,停止执行,不要猜测或暗中扩大任务范围。
- 如果scripts/main.py执行失败,报告失败点并总结仍可完成的内容。
- 不要虚构比例值或校准数据。
备用模板
当执行失败或输入不完整时,使用以下结构回复:
备用报告
───────────────────────────────────────
目标 : [重述目标]
阻塞原因 : [确切缺失的输入或错误]
部分结果 : [无需缺失输入即可完成的内容]
下一步 : [解除阻塞所需的最小操作]
───────────────────────────────────────
回复模板
- 1. 目标
- 收到的输入
- 假设条件
- 工作流程
- 交付物
- 风险与限制
- 后续检查
前置条件
需要Pillow:pip install Pillow