英文:
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格式:
package main
import (
"image"
"image/draw"
"image/jpeg"
"image/png"
"os"
)
func main() {
imgb, _ := os.Open("image.jpg")
img, _ := jpeg.Decode(imgb)
defer imgb.Close()
wmb, _ := os.Open("watermark.png")
watermark, _ := png.Decode(wmb)
defer wmb.Close()
offset := image.Pt(200, 200)
b := img.Bounds()
m := image.NewRGBA(b)
draw.Draw(m, b, img, image.ZP, draw.Src)
draw.Draw(m, watermark.Bounds().Add(offset), watermark, image.ZP, draw.Over)
imgw, _ := os.Create("watermarked.jpg")
jpeg.Encode(imgw, m, &jpeg.Options{jpeg.DefaultQuality})
defer imgw.Close()
}
image.jpg:
watermark.png:
result:
英文:
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:
package main
import (
"image"
"image/draw"
"image/jpeg"
"image/png"
"os"
)
func main() {
imgb, _ := os.Open("image.jpg")
img, _ := jpeg.Decode(imgb)
defer imgb.Close()
wmb, _ := os.Open("watermark.png")
watermark, _ := png.Decode(wmb)
defer wmb.Close()
offset := image.Pt(200, 200)
b := img.Bounds()
m := image.NewRGBA(b)
draw.Draw(m, b, img, image.ZP, draw.Src)
draw.Draw(m, watermark.Bounds().Add(offset), watermark, image.ZP, draw.Over)
imgw, _ := os.Create("watermarked.jpg")
jpeg.Encode(imgw, m, &jpeg.Options{jpeg.DefaultQuality})
defer imgw.Close()
}
image.jpg:
watermark.png:
result:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论