A Go-based image watermark tool that supports:
- Repeated tiled text watermark with adjustable spacing, angle, opacity, font size, and color.
- Single-position watermark with automatic foreground color based on local brightness and an outline stroke.
- JPEG-safe saving with background compositing.
go build ./cmd/watermarkRepeated watermark (requires font path):
./watermark -mode repeat \
-in input.jpg \
-out out.jpg \
-text "CONFIDENTIAL" \
-font /path/to/font.ttfPositioned watermark:
./watermark -mode position \
-in input.jpg \
-out out.jpg \
-text "CONFIDENTIAL"package main
import (
"image/color"
"watermark/pkg/watermark"
)
func main() {
colorHex := "#4db6ac"
space := 75
angle := 30
opacity := 0.5
fontSize := 48
fontHeightCrop := 1.0
_, _ = watermark.AddRepeatWatermark(
"input.jpg",
"out.jpg",
"CONFIDENTIAL",
&watermark.RepeatOptions{
Color: &colorHex,
Space: &space,
Angle: &angle,
Opacity: &opacity,
FontPath: "/path/to/font.ttf",
FontSize: &fontSize,
FontHeightCrop: &fontHeightCrop,
},
)
margin := 0.04
bg := colorNRGBA(255, 255, 255)
_, _ = watermark.AddPositionWatermark(
"input.jpg",
"out.jpg",
"CONFIDENTIAL",
&watermark.PositionOptions{
Opacity: &opacity,
Position: watermark.BottomRight,
MarginRatio: &margin,
JPGBackground: &bg,
},
)
}
func colorNRGBA(r, g, b uint8) color.NRGBA {
return color.NRGBA{R: r, G: g, B: b, A: 255}
}repeatmode requires a font path.positionmode tries the provided font, then common Arial locations, and finally falls back to the Go regular font.
- Chinese:
README.zh-CN.md