/
imageSign.go
64 lines (55 loc) · 1.24 KB
/
imageSign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"github.com/golang/freetype"
"image"
"image/color"
"image/png"
"io/ioutil"
"os"
)
const (
dx = 100
dy = 40
fontFile = "/Users/baike/Downloads/kaishu.ttf" // 需要使用的字体文件
fontSize = 20 // 字体尺寸
fontDPI = 72 // 屏幕每英寸的分辨率
)
func main() {
imgcounter := 123
imgfile, _ := os.Create(fmt.Sprintf("%03d.png", imgcounter))
defer imgfile.Close()
img := image.NewNRGBA(image.Rect(0, 0, dx, dy))
for y := 0; y < dy; y++ {
for x := 0; x < dx; x++ {
img.Set(x, y, color.RGBA{uint8(x), uint8(y), 0, 255})
}
}
fontBytes, err := ioutil.ReadFile(fontFile) //读取字体数据
if err != nil {
fmt.Println(err)
return
}
font, err := freetype.ParseFont(fontBytes)
if err != nil {
fmt.Print(err)
return
}
c := freetype.NewContext()
c.SetDPI(fontDPI)
c.SetFont(font)
c.SetFontSize(fontSize)
c.SetClip(img.Bounds())
c.SetDst(img)
c.SetSrc(image.White)
pt := freetype.Pt(10, 10+int(c.PointToFixed(fontSize)>>8)) // 字出现的位置
_, err = c.DrawString("ABCDE", pt)
if err != nil {
fmt.Println(err)
return
}
err = png.Encode(imgfile, img)
if err != nil {
fmt.Println(err)
}
}