Motyar

Freelance Web Developer

Sep 26, 2014

WaterMark Image with Golang

Go is powerful, Golang's package 'image' can be used to watermarning an image with another image.

Here is the code, I found on StackOverFlow:


package main

import (
    "image"
    "image/draw"
    "image/jpeg"
    "image/png"
    "os"
)

func main() {
    imgb, _ := os.Open("input.jpg")
    img, _ := jpeg.Decode(imgb)
    defer imgb.Close()

    wmb, _ := os.Open("watermark.png")
    watermark, _ := png.Decode(wmb)
    defer wmb.Close()

    offset := image.Pt(100, 100)
    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("output.jpg")
    jpeg.Encode(imgw, m, &jpeg.Options{jpeg.DefaultQuality})
    defer imgw.Close()
}

Labels: ,


By :