drawbox - 图像画框与文字标注工具
一个用 Go 编写的命令行工具,用于在图像上画矩形框并添加文字标签(支持中文)。
功能
- 图像加载和保存:支持 PNG、JPG、GIF、BMP 等常见格式
- 画框功能:在指定坐标位置画矩形框,支持自定义颜色与线宽
- 文字标注:在图像上绘制文字,支持中文,可设置字体大小、颜色和位置
- 字体支持:支持使用自定义 TrueType (.ttf) 字体文件,也提供内置字体
- 坐标解析:接受
x1,y1,x2,y2 格式的归一化坐标(0–1000),自动按图像尺寸换算为像素坐标
编译
go build -o drawbox .
用法
基本用法
# 显示帮助
./drawbox -help
# 仅画框
./drawbox -input img/in.jpg -output img/out.jpg -box 100,200,300,400 -width 3 -color green
# 仅添加文字(使用内置字体)
./drawbox -input img/in.jpg -output img/out.jpg -text "Hello World" -text-x 50 -text-y 100 -text-color blue -font-size 36
# 使用自定义字体添加文字
./drawbox -input img/in.jpg -output img/out.jpg -text "自定义字体" -text-x 100 -text-y 200 -text-color red -font-size 48 -font font/dinglie.ttf
# 同时画框和添加文字
./drawbox -input img/in.jpg -output img/out.jpg -box 100,100,500,500 -width 3 -color green -text "检测目标" -text-x 110 -text-y 90 -text-color yellow -font-size 32 -font font/dinglie.ttf
参数说明
矩形框选项
| 参数 |
说明 |
默认值 |
-box |
矩形框坐标,格式为 x1,y1,x2,y2(0-1000 的归一化坐标) |
无 |
-width |
框线宽度(像素) |
2 |
-color |
框线颜色 |
red |
文字选项
| 参数 |
说明 |
默认值 |
-text |
要绘制的文字内容 |
无 |
-text-x |
文字 X 坐标(像素) |
10 |
-text-y |
文字 Y 坐标(像素) |
30 |
-text-color |
文字颜色 |
red |
-font-size |
字体大小(像素) |
24 |
-font |
字体文件路径(.ttf 格式),不指定则使用内置字体 |
无 |
颜色格式
- 预设颜色:
red, green, blue, black, white, yellow, cyan, magenta
- 十六进制:
#RRGGBB(如 #FF0000 表示红色)
依赖
示例
画矩形框
./drawbox -input img/in.jpg -output img/out.jpg -box 100,200,300,400 -width 4 -color green
添加文字标注
./drawbox -input img/in.jpg -output img/out.jpg -text "检测结果" -text-x 50 -text-y 50 -text-color red -font-size 32 -font font/dinglie.ttf
组合使用
./drawbox -input img/in.jpg -output img/out.jpg \
-box 100,100,500,500 -width 3 -color green \
-text "目标区域" -text-x 110 -text-y 90 \
-text-color yellow -font-size 28 -font font/dinglie.ttf