qwertmax
qwertmax

Reputation: 3450

golang: print text in the image

I'm trying to use next packages

"image/draw"
"image"
"image/jpeg"

but I want to have possibility to print any text or numbers (which may also be text) in the my image.

But it looks like nothing from the box in Go to do this.

Can anyone help me with "GO way" solution for this?

Upvotes: 9

Views: 7304

Answers (2)

Yatender Singh
Yatender Singh

Reputation: 3322

check this

package main

import (
    "github.com/fogleman/gg"
    "log"
)

func main() {
    const S = 1024
    im, err := gg.LoadImage("src.jpg")
    if err != nil {
        log.Fatal(err)
    }

    dc := gg.NewContext(S, S)
    dc.SetRGB(1, 1, 1)
    dc.Clear()
    dc.SetRGB(0, 0, 0)
    if err := dc.LoadFontFace("/Library/Fonts/Arial.ttf", 96); err != nil {
        panic(err)
    }
    dc.DrawStringAnchored("Hello, world!", S/2, S/2, 0.5, 0.5)

    dc.DrawRoundedRectangle(0, 0, 512, 512, 0)
    dc.DrawImage(im, 0, 0)
    dc.DrawStringAnchored("Hello, world!", S/2, S/2, 0.5, 0.5)
    dc.Clip()
    dc.SavePNG("out.png")
}

Upvotes: 4

qwertmax
qwertmax

Reputation: 3450

I found just this one, freetype-go

is there a best and only lib for my needs?

Upvotes: 9

Related Questions