-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (48 loc) · 1006 Bytes
/
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
package main
import (
"github.com/nfnt/resize"
"image/jpeg"
"log"
"os"
"path/filepath"
"strconv"
)
func main() {
log.Println("Starting")
dir := os.Args[1]
dirpath := filepath.Join(dir, "*.jpg")
matches, err := filepath.Glob(dirpath)
if err != nil {
log.Fatal(err)
}
size := os.Args[2]
// twelve hundred or five hundred
dirResize := filepath.Join(dir, size)
os.Mkdir(dirResize, 0755)
log.Println(len(matches))
for _, value := range matches {
file, err := os.Open(value)
if err != nil {
log.Fatal(err)
}
img, err := jpeg.Decode(file)
if err != nil {
log.Fatal(err)
}
file.Close()
sizeInt, err := strconv.ParseUint(size, 10, 64)
if err != nil {
log.Fatal(err)
}
m := resize.Resize(uint(sizeInt), 0, img, resize.Lanczos3)
basename := filepath.Base(value)
newpath := filepath.Join(dirResize, basename)
out, err := os.Create(newpath)
if err != nil {
log.Fatal(err)
}
jpeg.Encode(out, m, nil)
out.Close()
log.Println(newpath)
}
}