How to add Watermark or Merge two image?
In below example image2 will added as Watermark over image1 as a result image3 would be a new image. This is a simple example to merge 2 images.
Example
package main
import (
"image"
"image/draw"
"image/jpeg"
"image/png"
"os"
"log"
)
func main() {
image1,err := os.Open("jellyfish.jpg")
if err != nil {
log.Fatalf("failed to open: %s", err)
}
first, err := jpeg.Decode(image1)
if err != nil {
log.Fatalf("failed to decode: %s", err)
}
defer image1.Close()
image2,err := os.Open("pokeball.png")
if err != nil {
log.Fatalf("failed to open: %s", err)
}
second,err := png.Decode(image2)
if err != nil {
log.Fatalf("failed to decode: %s", err)
}
defer image2.Close()
offset := image.Pt(300, 200)
b := first.Bounds()
image3 := image.NewRGBA(b)
draw.Draw(image3, b, first, image.ZP, draw.Src)
draw.Draw(image3, second.Bounds().Add(offset), second, image.ZP, draw.Over)
third,err := os.Create("result.jpg")
if err != nil {
log.Fatalf("failed to create: %s", err)
}
jpeg.Encode(third, image3, &jpeg.Options{jpeg.DefaultQuality})
defer third.Close()
}
Most Helpful This Week
Creating a Function in Golang
Regular expression to extract all Non-Alphanumeric Characters from a String
Simple function with parameters in Golang
How to read names of all files and folders in current directory?
How to set, get, and list environment variables?
Convert Float32 to Float64 and Float64 to Float32