-
Notifications
You must be signed in to change notification settings - Fork 44
/
tti.go
61 lines (52 loc) · 1.13 KB
/
tti.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
package main
import (
"fmt"
"github.com/golang/freetype"
"github.com/leoython/text-to-video/internal"
"image"
"image/draw"
"image/jpeg"
"io/ioutil"
"os"
)
const (
defaultSize float64 = 45
defaultSpacing float64 = 1.5
)
type Imager struct {}
func(i *Imager) genBaseImage(filename, text string) error {
m := image.NewRGBA(image.Rect(0, 0, 1600, 900))
draw.Draw(m, m.Bounds(), image.Black, image.ZP, draw.Src)
if err := internal.CreateFolderIfNotExists("image"); err != nil {
return err
}
// Read the font data.
fontBytes, err := ioutil.ReadFile("./front/SourceHanSans-Bold.ttf")
if err != nil {
return err
}
f, err := freetype.ParseFont(fontBytes)
if err != nil {
return err
}
c := freetype.NewContext()
c.SetDPI(72)
c.SetFont(f)
c.SetFontSize(defaultSize)
c.SetClip(m.Bounds())
c.SetDst(m)
c.SetSrc(image.White)
pt := freetype.Pt(350, 200+int(c.PointToFixed(defaultSize)>>6))
if _, err := c.DrawString(text, pt); err != nil {
return err
}
fd, err := os.Create(fmt.Sprintf("image/%s.jpeg", filename))
if err != nil {
return err
}
err = jpeg.Encode(fd, m, nil)
if err != nil {
return err
}
return nil
}