Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating watermark images with Go

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.

like image 532
Goper-Alex Avatar asked Apr 19 '13 08:04

Goper-Alex


1 Answers

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:

enter image description here

watermark.png:

enter image description here

result:

enter image description here

like image 68
Nicolas Kaiser Avatar answered Oct 22 '22 17:10

Nicolas Kaiser