使用Go语言处理水印图像

huangapple go评论126阅读模式
英文:

Manipulating watermark images with Go

问题

我想找一些关于使用Go语言制作水印图像的示例。

我需要一个PNG图像作为水印图像,可以应用于其他格式的PNG、GIF、JPEG等。

希望你能给我一些实际的例子。

英文:

I want to find something about making the watermark image examples, written in Go language!

I need a PNG image for the watermark image, that can be applied to other formats PNG, GIF, JPEG etc.,

I hope you can give me some practical examples.

答案1

得分: 36

如前所述,您可以使用image/draw包给图像添加水印。

以下是一个快速实际示例,将一个透明的png图像添加到一个jpeg图像中并保存为jpeg格式:

  1. package main
  2. import (
  3. "image"
  4. "image/draw"
  5. "image/jpeg"
  6. "image/png"
  7. "os"
  8. )
  9. func main() {
  10. imgb, _ := os.Open("image.jpg")
  11. img, _ := jpeg.Decode(imgb)
  12. defer imgb.Close()
  13. wmb, _ := os.Open("watermark.png")
  14. watermark, _ := png.Decode(wmb)
  15. defer wmb.Close()
  16. offset := image.Pt(200, 200)
  17. b := img.Bounds()
  18. m := image.NewRGBA(b)
  19. draw.Draw(m, b, img, image.ZP, draw.Src)
  20. draw.Draw(m, watermark.Bounds().Add(offset), watermark, image.ZP, draw.Over)
  21. imgw, _ := os.Create("watermarked.jpg")
  22. jpeg.Encode(imgw, m, &jpeg.Options{jpeg.DefaultQuality})
  23. defer imgw.Close()
  24. }

image.jpg:

使用Go语言处理水印图像

watermark.png:

使用Go语言处理水印图像

result:

使用Go语言处理水印图像

英文:

As already mentioned, you can watermark images with the image/draw package.

Here's a quick practical example, adding a transparent png image to a jpeg image and saving as jpeg:

  1. package main
  2. import (
  3. "image"
  4. "image/draw"
  5. "image/jpeg"
  6. "image/png"
  7. "os"
  8. )
  9. func main() {
  10. imgb, _ := os.Open("image.jpg")
  11. img, _ := jpeg.Decode(imgb)
  12. defer imgb.Close()
  13. wmb, _ := os.Open("watermark.png")
  14. watermark, _ := png.Decode(wmb)
  15. defer wmb.Close()
  16. offset := image.Pt(200, 200)
  17. b := img.Bounds()
  18. m := image.NewRGBA(b)
  19. draw.Draw(m, b, img, image.ZP, draw.Src)
  20. draw.Draw(m, watermark.Bounds().Add(offset), watermark, image.ZP, draw.Over)
  21. imgw, _ := os.Create("watermarked.jpg")
  22. jpeg.Encode(imgw, m, &jpeg.Options{jpeg.DefaultQuality})
  23. defer imgw.Close()
  24. }

image.jpg:

使用Go语言处理水印图像

watermark.png:

使用Go语言处理水印图像

result:

使用Go语言处理水印图像

huangapple
  • 本文由 发表于 2013年4月19日 16:02:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/16100023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定