commit 5507fd209b573b9484c04eb5590ae00d3c598528 Author: suguo.yao Date: Wed Jan 12 17:59:12 2022 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f5888c --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +txt2img-demo +logs/ +bin/ +.vscode/ +*.exe +go.sum +*.ttf +*.jpg +*.png \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..53047dc --- /dev/null +++ b/go.mod @@ -0,0 +1,13 @@ +module myschools.me/suguo/txt2img + +go 1.17 + +require ( + github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 + github.com/hqbobo/text2pic v0.0.0-20180823042751-2479e146d720 +) + +require ( + github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect + golang.org/x/image v0.0.0-20211028202545-6944b10bf410 // indirect +) diff --git a/main.go b/main.go new file mode 100644 index 0000000..77d9853 --- /dev/null +++ b/main.go @@ -0,0 +1,67 @@ +package main + +import ( + "bufio" + "fmt" + "io/ioutil" + "log" + "os" + + "github.com/golang/freetype" + "github.com/hqbobo/text2pic" +) + +func main() { + + // Read the font data. + fontBytes, err := ioutil.ReadFile("NewYork.ttf") + if err != nil { + log.Println(err) + return + } + //produce the fonttype + f, err := freetype.ParseFont(fontBytes) + if err != nil { + log.Println(err) + return + } + + //define New picture with given width in px + //the height will be calucated before draw on picture + //picture will be resize to 80% of the width you given + pic := text2pic.NewTextPicture(text2pic.Configure{ + Width: 720, + }) + + //add chinese line + pic.AddTextLine("1.这个是标题", 20, f, text2pic.ColorRed, text2pic.Padding{Left: 20, Top: 10, Bottom: 20}) + pic.AddTextLine(" 北京铁路局今天凌晨2时16分发布消息称:8月12日23时04分,aaaa京沪高铁廊坊至北京aaaaa南间发生设备故障,导致部分列车晚点。铁路部门及时启动应急预案处置时16分发布消息称时16分发布消息称北京铁路局今天凌晨2时16分发布消息称:8月12日23时04分,aaaa京沪高铁廊坊至北京aaaaa南间发生设备故障,导致部分列车晚点。铁路部门及时启动应急预案处置时16分发布消息称时16分发布消息称北京铁路局今天凌晨2时16分发布消息称:8月12日23时04分,aaaa京沪高铁廊坊至北京aaaaa南间发生设备故障,导致部分列车晚点。铁路部门及时启动应急预案处置时16分发布消息称时16分发布消息称", 12, f, text2pic.ColorGreen, text2pic.Padding{Left: 20, Right: 20, Bottom: 30}) + //add picture + // file, err := os.Open("download.png") + // if err != nil { + // fmt.Println(err) + // } + // defer file.Close() + + // pic.AddPictureLine(file, text2pic.Padding{Bottom: 20}) + + // //add full english text + // pic.AddTextLine("3.For English", 20, f, text2pic.ColorRed, text2pic.Padding{Bottom: 20}) + // pic.AddTextLine(" The Turkish lira plunged as much as 11% against the dollar, hitting a record low, before recovering some of its losses in volatile trading. The lira had already plummeted more than 20% last week as a political clash with the United States intensified and investors fretted about the Turkish government's lack of action to tackle the problems plaguing its economy. ", 13, f, text2pic.ColorBlue, text2pic.Padding{Left: 20, Right: 20, Bottom: 30}) + + // Save the output to file + outFile, err := os.Create("out.jpg") + if err != nil { + return + } + defer outFile.Close() + b := bufio.NewWriter(outFile) + //produce the output + pic.Draw(b, text2pic.TypeJpeg) + // pic.Draw(b, text2pic.TypePng) + e := b.Flush() + if e != nil { + fmt.Println(e) + } + +} diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..dd39822 --- /dev/null +++ b/readme.md @@ -0,0 +1,6 @@ +## 文字转换为图片示例 + +--- + +### 1. 组件 + https://github.com/hqbobo/text2pic \ No newline at end of file diff --git a/simple/main.go b/simple/main.go new file mode 100644 index 0000000..2544b50 --- /dev/null +++ b/simple/main.go @@ -0,0 +1,66 @@ +package main + +import ( + "bufio" + "fmt" + "image" + "image/draw" + "image/png" + "io/ioutil" + "log" + "os" + + "github.com/golang/freetype" +) + +func main() { + + // Read the font data. + fontBytes, err := ioutil.ReadFile("../NewYork.ttf") + if err != nil { + log.Println(err) + return + } + //produce the fonttype + f, err := freetype.ParseFont(fontBytes) + if err != nil { + log.Println(err) + return + } + + fg, bg := image.Black, image.White + rgba := image.NewRGBA(image.Rect(0, 0, 640, 480)) + draw.Draw(rgba, rgba.Bounds(), bg, image.ZP, draw.Src) + + c := freetype.NewContext() + // 设置像素密度 + c.SetDPI(140) + // 指定字体 + c.SetFont(f) + // 指定字体大小 + c.SetFontSize(140) + // 指定画布对象 + c.SetDst(rgba) + // 指定画布绘制范围 + c.SetClip(rgba.Bounds()) + // 指定文字颜色 + c.SetSrc(fg) + + pt := freetype.Pt(10, 10+int(c.PointToFixed(140)>>6)) + //add chinese line + c.DrawString("B", pt) + // Save the output to file + of, err := os.Create("out.png") + if err != nil { + fmt.Println(err.Error()) + } + defer of.Close() + b := bufio.NewWriter(of) + if err := png.Encode(b, rgba); err != nil { + fmt.Println(err.Error()) + } + if err := b.Flush(); err != nil { + fmt.Println(err.Error()) + } + +}