16

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
  • See this article for image manipulation: golang.org/doc/articles/image_draw.html . Some of the examples seem to cover your needs, essentially blending some image into another with alpha transparency.
    – Mikke
    Apr 19, 2013 at 12:26

1 Answer 1

36

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.