9

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?

qwertmax
  • 3,120
  • 2
  • 29
  • 42
  • I am guessing that you want a all on one solution, so that you read a jpeg image, add text to it and then save it as a jpeg image? The only thing i can think off, is using Azul3d, it contains the option to read jpeg images and display it, use freetype to display text. But i am unsure if there is a option to save the image with the included text. – Dippo Dec 23 '14 at 10:41
  • azul3d - is a game engine, I'm not sure that it is best thing for adding text to images. – qwertmax Dec 25 '14 at 10:13
  • Check out this possible duplicate: [How to add a simple text label to an image in Go?](http://stackoverflow.com/questions/38299930/how-to-add-a-simple-text-label-to-an-image-in-go) – icza Mar 07 '17 at 14:23

2 Answers2

9

I found just this one, freetype-go

is there a best and only lib for my needs?

qwertmax
  • 3,120
  • 2
  • 29
  • 42
4

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")
}
Yatender Singh
  • 3,098
  • 3
  • 22
  • 31