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?

3
  • 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
    Commented Dec 23, 2014 at 10:41
  • azul3d - is a game engine, I'm not sure that it is best thing for adding text to images.
    – qwertmax
    Commented Dec 25, 2014 at 10:13
  • Check out this possible duplicate: How to add a simple text label to an image in Go?
    – icza
    Commented Mar 7, 2017 at 14:23

2 Answers 2

9

I found just this one, freetype-go

is there a best and only lib for my needs?

0
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")
}

Your Answer

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

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