-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackGround.go
42 lines (38 loc) · 1019 Bytes
/
backGround.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
package text2picture
import (
"image"
"image/color"
"image/draw"
"image/png"
"io"
"os"
)
// Color can be generated using text2picture.NewColor()
func NewColorPicture(width, height int, color color.Color) *image.RGBA {
rgba := image.NewRGBA(image.Rect(0, 0, width, height))
draw.Draw(rgba, rgba.Rect, image.NewUniform(color), image.Point{}, draw.Src)
return rgba
}
func LoadPicture(filepath string) (*image.RGBA, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer f.Close()
bac, err := png.Decode(f)
if err != nil {
return nil, err
}
rgba := image.NewRGBA(image.Rect(0, 0, bac.Bounds().Max.X, bac.Bounds().Max.Y))
draw.Src.Draw(rgba, rgba.Rect, bac, bac.Bounds().Min)
return rgba, nil
}
func ReadPicture(file io.Reader) (*image.RGBA, error) {
bac, err := png.Decode(file)
if err != nil {
return nil, err
}
rgba := image.NewRGBA(image.Rect(0, 0, bac.Bounds().Max.X, bac.Bounds().Max.Y))
draw.Src.Draw(rgba, rgba.Rect, bac, bac.Bounds().Min)
return rgba, nil
}