Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang: print text in the image

Tags:

text

image

go

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?

like image 968
qwertmax Avatar asked Dec 23 '14 09:12

qwertmax


People also ask

How to print a string in Golang with spaces?

The fmt.print () function in Go language formats using the default formats for its operands and writes to standard output. Here Spaces are added between operands when any string is not used as a parameter. Moreover, this function is defined under the fmt package.

How to draw text on an image in Golang?

The golang.org/x/image/font package just defines interfaces for font faces and drawing text on images. You may use the Go implementation of Freetype font rasterizer: github.com/golang/freetype. The key type is freetype.Context, it has all the methods you need. For a complete example, check out this file: example/freetype/main.go.

What is png in Golang?

png – png implements a PNG image decoder and encoder. So we’ll dive into the png library, and the rest are just similar implementations for various formats. GoLang png package implements a PNG image decoder and encoder. A wonderful implementation of converting an image from base64 string to png, and then printing it with 5 levels of lines:

How to print a string in Go language format?

The fmt.print () function in Go language formats using the default formats for its operands and writes to standard output. Here Spaces are added between operands when any string is not used as a parameter.


2 Answers

I found just this one, freetype-go

is there a best and only lib for my needs?

like image 59
qwertmax Avatar answered Oct 25 '22 03:10

qwertmax


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")
}
like image 4
Yatender Singh Avatar answered Oct 25 '22 04:10

Yatender Singh