原始图片:
添加水印后的图片:
//golang图片式水印处理 //未实现Web上传等代码... //sam //2013.6.19 //http://zituo.net package main import ( "fmt" "image" "image/draw" "image/jpeg" "image/png" "os" ) func main() { //原始图片是sam.jpg imgb,_ := os.Open("sam.jpg") img,_ := jpeg.Decode(imgb) defer imgb.Close() wmb,_ := os.Open("text.png") watermark,_ := png.Decode(wmb) defer wmb.Close() //把水印写到右下角,并向0坐标各偏移10个像素 offset := image.Pt(img.bounds().Dx()-watermark.bounds().Dx()-10,img.bounds().Dy()-watermark.bounds().Dy()-10) b := img.bounds() m := image.NewNRGBA(b) draw.Draw(m,b,img,image.ZP,draw.SrC) draw.Draw(m,watermark.bounds().Add(offset),watermark,draw.over) //生成新图片new.jpg,并设置图片质量.. imgw,_ := os.Create("new.jpg") jpeg.Encode(imgw,m,&jpeg.options{100}) defer imgw.Close() fmt.Println("水印添加结束,请查看new.jpg图片...") }