专栏首页Golang语言社区Golang 图片上绘制文字

Golang 图片上绘制文字

package main
import ( 
    "bufio" 
    "flag" 
    "fmt" 
    "image" 
    "image/color" 
    "image/draw" 
    "image/png" 
    "io/ioutil" 
    "log" 
    "os"
    "github.com/golang/freetype" 
    "golang.org/x/image/font" 
)
var ( 
    dpi      = flag.Float64("dpi", 72, "screen resolution in Dots Per Inch") 
    fontfile = flag.String("fontfile", "../../testdata/luxisr.ttf", "filename of the ttf font") 
    hinting  = flag.String("hinting", "none", "none | full") 
    size     = flag.Float64("size", 12, "font size in points") 
    spacing  = flag.Float64("spacing", 1.5, "line spacing (e.g. 2 means double spaced)") 
    wonb     = flag.Bool("whiteonblack", false, "white text on a black background") 
)
var text = []string{ 
    "’Twas brillig, and the slithy toves", 
    "测试中文字 ……&^_^", 
    "Did gyre and gimble in the wabe;", 
    "All mimsy were the borogoves,", 
    "And the mome raths outgrabe.", 
    "", 
    "“Beware the Jabberwock, my son!", 
    "The jaws that bite, the claws that catch!", 
    "Beware the Jubjub bird, and shun", 
    "The frumious Bandersnatch!”", 
    "", 
    "He took his vorpal sword in hand:", 
    "Long time the manxome foe he sought—", 
    "So rested he by the Tumtum tree,", 
    "And stood awhile in thought.", 
    "", 
    "And as in uffish thought he stood,", 
    "The Jabberwock, with eyes of flame,", 
    "Came whiffling through the tulgey wood,", 
    "And burbled as it came!", 
    "", 
    "One, two! One, two! and through and through", 
    "The vorpal blade went snicker-snack!", 
    "He left it dead, and with its head", 
    "He went galumphing back.", 
    "", 
    "“And hast thou slain the Jabberwock?", 
    "Come to my arms, my beamish boy!", 
    "O frabjous day! Callooh! Callay!”", 
    "He chortled in his joy.", 
    "", 
    "’Twas brillig, and the slithy toves", 
    "Did gyre and gimble in the wabe;", 
    "All mimsy were the borogoves,", 
    "And the mome raths outgrabe.", 
}
func main() { 
 flag.Parse()
    // Read the font data. 
    fontBytes, err := ioutil.ReadFile(*fontfile) 
    if err != nil { 
        log.Println(err) 
        return 
    } 
    f, err := freetype.ParseFont(fontBytes) 
    if err != nil { 
        log.Println(err) 
        return 
    }
    // Initialize the context. 
    fg, bg := image.Black, image.White 
    ruler := color.RGBA{0xdd, 0xdd, 0xdd, 0xff} 
    if *wonb { 
        fg, bg = image.White, image.Black 
        ruler = color.RGBA{0x22, 0x22, 0x22, 0xff} 
    } 
    rgba := image.NewRGBA(image.Rect(0, 0, 640, 480)) 
    draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src) 
    c := freetype.NewContext() 
    c.SetDPI(*dpi) 
    c.SetFont(f) 
    c.SetFontSize(*size) 
    c.SetClip(rgba.Bounds()) 
    c.SetDst(rgba) 
    c.SetSrc(fg) 
 switch *hinting { 
    default: 
        c.SetHinting(font.HintingNone) 
    case "full": 
        c.SetHinting(font.HintingFull) 
    }
    // Draw the guidelines. 
    for i := 0; i < 200; i++ { 
        rgba.Set(10, 10+i, ruler) 
        rgba.Set(10+i, 10, ruler) 
    }
    // Draw the text. 
    pt := freetype.Pt(10, 10+int(c.PointToFixed(*size)>>6)) 
    for _, s := range text { 
        _, err = c.DrawString(s, pt) 
        if err != nil { 
            log.Println(err) 
            return 
        } 
        pt.Y += c.PointToFixed(*size * *spacing) 
    }
    // Save that RGBA image to disk. 
    outFile, err := os.Create("out.png") 
    if err != nil { 
        log.Println(err) 
        os.Exit(1) 
    } 
    defer outFile.Close() 
    b := bufio.NewWriter(outFile) 
    err = png.Encode(b, rgba) 
    if err != nil { 
        log.Println(err) 
        os.Exit(1) 
    } 
    err = b.Flush() 
    if err != nil { 
        log.Println(err) 
        os.Exit(1) 
    } 
    fmt.Println("Wrote out.png OK.") 
} 

本文分享自微信公众号 - Golang语言社区(Golangweb)

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2016-03-11

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Linux服务器最最基本安全策略

    1、Linux SSH 安全策略一:关闭无关端口 网络上被攻陷的大多数主机,是黑客用扫描工具大范围进行扫描而被瞄准上的。所以,为了避免被扫描到,除了必要的端口,...

    李海彬
  • TCP、UDP、IP 协议分析

    互连网早期的时候,主机间的互连使用的是NCP协议。这种协议本身有很多缺陷,如:不能互连不同的主机,不能互连不同的操作系统,没有纠错功能。为了改善这种缺点,大牛弄...

    李海彬
  • GO语言 TCP传输实例

    package main import ( "net" "fmt" ) var ( maxRead = 1100 msgStop = []byt...

    李海彬
  • 还在用PPT做神经网络架构图?GitHub2万星项目,手把手教你从入门到可视化大师

    新智元的同事身边有很多做机器学习的同学,他们有个共通点就是:撸代码时候挺爽的,一旦要将过程和结果可视化的时候,就有点犯怵。

    新智元
  • Backbone.js 简介

    Backbone.js提供模型(models)、集合(collections)、视图(views)结构。

    一个会写诗的程序员
  • 速读原著-TCP/IP(长肥管道)

    并称之为带宽时延乘积。也可称它为两端的管道大小。当这个乘积变得越来越大时, T C P的某些局限性就会暴露出来。图 2 4 - 5显示了多种类型的网络的某些数值...

    cwl_java
  • 谈谈Vue开发过程中用到的插件

    在我们的平时开发过程中,为了高效的提示开发效率和缩短开发的时间,这时我们会想到使用一些周边的插件,今天小编整理了一下自己在开发过程中使用的插件,不仅是对知识的梳...

    小丑同学
  • Unity项目嵌入现有iOS项目的方法

    傅_hc
  • 使用 Sphinx 给 Python 项目生成【Read the Docs】在线文档

    “Read the Docs” 提供自动构建,版本控制和在线托管,来简化软件文档的发布和管理。它使用 Sphinx 生成 html 静态页面,通过 github...

    我是一条小青蛇
  • PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分)

    Notice that the number 123456789 is a 9-digit number consisting exactly the numb...

    glm233

扫码关注云+社区

领取腾讯云代金券