【问题标题】:How to add a simple text label to an image in Go?如何在 Go 中为图像添加简单的文本标签?
【发布时间】:2016-11-12 23:40:51
【问题描述】:

给定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?
}

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

而且我想避免像字体这样的外部可加载依赖项。

【问题讨论】:

  • 这个包明确声明它只提供一个字体界面。 "Other packages provide font face implementations. For example, a truetype package would provide one based on .ttf font files."

标签: image go fonts draw


【解决方案1】:
推荐的答案 Go Language

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 坐标将是绘制文本的顶部坐标。

golang.org/x/image/font/inconsolata 包中还有一个额外的自包含字体,具有常规和粗体样式,要使用它们,您只需在addLabel() 中指定不同的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,

【讨论】:

  • @sanmai 查看已编辑的答案,我提供了一个如何使用 basicfont.Face7x13 的示例。
  • 谢谢,差不多了!只有它打印行half cut
  • @sanmai 你的img 你传递的够大吗?可能是因为您将文本放置在图像底部下方而被剪切了?
  • One or more of the answers is exemplary and worthy of an additional bounty. 确实!
  • 有一个较新的example/drawer/main.go,我敦促任何想要使用它的人检查一下 - 它具有一些更新的功能,例如 MeasureText,这让事情变得更加容易。
【解决方案2】:

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

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

【讨论】:

  • 在 raspberrypi 中执行 dc.DrawImage(im, 0, 0) 速度很慢,我的功能超时。能够通过使用 gg.NewContextForImage("src.jpg") 来加快速度
猜你喜欢
  • 2011-04-19
  • 2019-12-05
  • 1970-01-01
  • 2018-09-22
  • 2020-06-18
  • 2013-03-23
  • 2019-09-30
  • 2021-05-09
  • 1970-01-01
相关资源
最近更新 更多