如何在 Go 中为图像添加简单的文本标签?

How to add a simple text label to an image in Go?

给定 image.RGBA、坐标和一行文本,我如何添加一个简单的带有任何固定字体的简单标签?例如。 Face7x13 来自 font/basicfont.

package main

import (
    "image"
    "image/color"
    "image/png"
    "os"
)

func main() {
    img := image.NewRGBA(image.Rect(0, 0, 320, 240))
    x, y := 100, 100
    addLabel(img, x, y, "Test123")
    png.Encode(os.Stdout, img)
}

func addLabel(img *image.RGBA, x, y int, label string) {
     col := color.Black
     // now what?
}

对齐并不重要,但最好是将标签写在从坐标开始的直线上方。

而且我想避免外部可加载依赖项,例如字体。

golang.org/x/image/font 包只定义了字体和图像上绘图文本的接口。

您可以使用 Freetype 字体光栅器的 Go 实现:github.com/golang/freetype

密钥类型是freetype.Context,它有你需要的所有方法。

有关完整示例,请查看此文件:example/freetype/main.go。此示例加载字体文件,创建和配置 freetype.Context,在图像上绘制文本并将结果图像保存到文件。

假设您已经加载了字体文件,并配置了 c 上下文(请参阅示例如何操作)。那么您的 addLabel() 函数可能如下所示:

func addLabel(img *image.RGBA, x, y int, label string) {
    c.SetDst(img)
    size := 12.0 // font size in pixels
    pt := freetype.Pt(x, y+int(c.PointToFixed(size)>>6))

    if _, err := c.DrawString(label, pt); err != nil {
        // handle error
    }
}

如果您不想麻烦 freetype 包和外部字体文件,font/basicfont 包包含一个名为 Face7x13 的基本字体,其图形数据完全是自包含。你可以这样使用它:

import (
    "golang.org/x/image/font"
    "golang.org/x/image/font/basicfont"
    "golang.org/x/image/math/fixed"
    "image"
    "image/color"
)

func addLabel(img *image.RGBA, x, y int, label string) {
    col := color.RGBA{200, 100, 0, 255}
    point := fixed.Point26_6{fixed.I(x), fixed.I(y)}

    d := &font.Drawer{
        Dst:  img,
        Src:  image.NewUniform(col),
        Face: basicfont.Face7x13,
        Dot:  point,
    }
    d.DrawString(label)
}

这是 addLabel() 函数的用法:下面的代码创建一个新图像,在其上绘制 "Hello Go" 文本并将其保存在名为 hello-go.png 的文件中:

func main() {
    img := image.NewRGBA(image.Rect(0, 0, 300, 100))
    addLabel(img, 20, 30, "Hello Go")

    f, err := os.Create("hello-go.png")
    if err != nil {
        panic(err)
    }
    defer f.Close()
    if err := png.Encode(f, img); err != nil {
        panic(err)
    }
}

注意上面的代码还需要 "image/png" 包导入。

另请注意,给定的 y 坐标将作为文本的底线。所以如果你想画一条线到左上角,你必须使用x = 0y = 13(13是这个Face7x13字体的高度)。如果您愿意,可以通过从 y 坐标中减去 13 将其构建到 addLabel() 函数中,这样传递的 y 坐标将成为顶部坐标将绘制文本。

addLabel()中的golang.org/x/image/font/inconsolata package with regular and bold style, to use them, you only need to specify a different Face中还有一个额外的自带字体:

import "golang.org/x/image/font/inconsolata"

        // To use regular Inconsolata font family:
        Face: inconsolata.Regular8x16,

        // To use bold Inconsolata font family:
        Face: inconsolata.Bold8x16,

这里是使用 gg 库的示例代码,我们已经有了 src.jpg 或任何图像,我们在上面写了文字。您可以相应地调整 canvas 大小。这只是示例。如果它不起作用,请告诉我。

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