/
goresize.go
61 lines (51 loc) · 1.09 KB
/
goresize.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"
"image"
"log"
"github.com/disintegration/imaging"
"github.com/jteeuwen/go-pkg-optarg"
)
func main() {
optarg.Header("General options")
optarg.Add("h", "height", "image height", 100)
optarg.Add("w", "width", "image width", 0)
optarg.Add("i", "input", "input filename", "")
optarg.Add("o", "output", "output filename", "")
var (
height int
width int
inName string
outName string
)
for opt := range optarg.Parse() {
switch opt.ShortName {
case "h":
height = opt.Int()
case "w":
width = opt.Int()
case "i":
inName = opt.String()
case "o":
outName = opt.String()
}
}
if inName == "" || outName == "" {
optarg.Usage()
log.Fatal()
}
orgImg, err := imaging.Open(inName)
if err != nil {
log.Fatal(err)
}
dstImg := resize(orgImg, width, height)
err = imaging.Save(dstImg, outName)
if err != nil {
log.Fatal(err)
}
fmt.Println("Resize to", outName)
}
func resize(img image.Image, width int, height int) image.Image {
dstImg := imaging.Resize(img, width, height, imaging.Lanczos)
return dstImg.SubImage(dstImg.Rect)
}