-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
71 lines (58 loc) · 1.4 KB
/
main.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
65
66
67
68
69
70
71
package main
import (
"fmt"
"image"
"image/draw"
"image/jpeg"
"image/png"
"log"
"os"
"github.com/nfnt/resize"
)
func main() {
for _, i := range []int{1, 2, 3, 4} {
path, err := AddFlag("avatar.jpeg", i)
if err != nil {
log.Println(err)
}
log.Println(path)
}
}
func AddFlag(avatarPath string, num int) (scrName string, err error) {
paddingX, paddingY := 20, 20 //图片边框长度
scrName = fmt.Sprintf("./output/flag_avatar%d.png", num)
flagPath := fmt.Sprintf("./src/flag%d.png", num)
file, err := os.Create(scrName)
if err != nil {
return
}
defer file.Close()
flagFile, err := os.Open(flagPath)
if err != nil {
return
}
defer flagFile.Close()
flagImg, err := png.Decode(flagFile)
if err != nil {
return
}
avatarFile, err := os.Open(avatarPath)
if err != nil {
return
}
defer avatarFile.Close()
avatarImg, err := jpeg.Decode(avatarFile)
if err != nil {
return
}
//resize avatar
newImage := resize.Resize(uint(flagImg.Bounds().Dx()-20), uint(flagImg.Bounds().Dy()-20), avatarImg, resize.Lanczos3)
resultPng := image.NewRGBA(image.Rect(0, 0, flagImg.Bounds().Dx(), flagImg.Bounds().Dy()))
draw.Draw(resultPng, resultPng.Bounds(), newImage, newImage.Bounds().Min.Sub(image.Pt(paddingX, paddingY)), draw.Over)
draw.Draw(resultPng, resultPng.Bounds(), flagImg, flagImg.Bounds().Min, draw.Over)
err = png.Encode(file, resultPng)
if err != nil {
return
}
return
}