This is a creation in Article, where the information may have evolved or changed.
//golang图片式水印处理//未实现Web上传等代码...//sam//2013.6.19//http://zituo.netpackage mainimport ("fmt""image""image/draw""image/jpeg""image/png""os")func main() {//原始图片是sam.jpgimgb, _ := 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, image.ZP, draw.Over)//生成新图片new.jpg,并设置图片质量..imgw, _ := os.Create("new.jpg")jpeg.Encode(imgw, m, &jpeg.Options{100})defer imgw.Close()fmt.Println("水印添加结束,请查看new.jpg图片...")}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.