/
main.go
106 lines (84 loc) · 1.9 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"errors"
"flag"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"github.com/nfnt/resize"
)
const PATH = "resized"
var y = flag.Float64("y", 0, "desired height, 0 for auto, 0 > y > 1 for percentage")
var x = flag.Float64("x", 0, "desired width, 0 for auto, 0 > x > 1 for percentage")
func main() {
flag.Parse()
if err := validate(*x, *y, flag.Args()); err != nil {
fmt.Println(err)
usage()
return
}
files, err := filepath.Glob(flag.Args()[0])
if err != nil {
fmt.Println(err)
return
}
os.Mkdir(PATH, 0777)
for _, file := range files {
resizeFile(file, *x, *y)
}
}
func validate(x, y float64, args []string) error {
if x == 0 && y == 0 {
return errors.New("specify either x or y flag or both")
}
if x < 0 || y < 0 {
return errors.New("x and y flags must be positive values")
}
if len(args) != 1 {
return errors.New("specify filename or wildcard of filenames to resize")
}
return nil
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage example:\n\tresizer -x=0.3 *.jpg\n\n")
fmt.Fprintf(os.Stderr, "All possible flags:\n")
flag.CommandLine.PrintDefaults()
}
func resizeFile(filename string, xPx, yPx float64) {
fmt.Println("Processing", filename)
file, err := os.Open(filename)
check(err)
defer file.Close()
img, imgFmt, err := image.Decode(file)
check(err)
if xPx < 1 {
xPx = float64(img.Bounds().Size().X) * xPx
}
if yPx < 1 {
yPx = float64(img.Bounds().Size().Y) * yPx
}
m := resize.Resize(uint(xPx), uint(yPx), img, resize.Bilinear)
out, err := os.Create(PATH + "/" + filename)
check(err)
defer out.Close()
if imgFmt == "jpeg" {
err = jpeg.Encode(out, m, nil)
} else if imgFmt == "gif" {
err = gif.Encode(out, m, nil)
} else if imgFmt == "png" {
err = png.Encode(out, m)
} else {
fmt.Println("Unrecognized format:", imgFmt)
return
}
check(err)
}
func check(err error) {
if err != nil {
panic(err)
}
}