Skip to content

lz-wang/Watermark

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Watermark

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.

Build

go build ./cmd/watermark

CLI Usage

Repeated watermark (requires font path):

./watermark -mode repeat \
  -in input.jpg \
  -out out.jpg \
  -text "CONFIDENTIAL" \
  -font /path/to/font.ttf

Positioned watermark:

./watermark -mode position \
  -in input.jpg \
  -out out.jpg \
  -text "CONFIDENTIAL"

Library Usage

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}
}

Notes

  • repeat mode requires a font path.
  • position mode tries the provided font, then common Arial locations, and finally falls back to the Go regular font.

Other Languages

  • Chinese: README.zh-CN.md

About

Image Watermark Tool

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages