-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
126 lines (103 loc) · 2.22 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bufio"
"fmt"
"image"
"image/jpeg"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
"github.com/nfnt/resize"
)
func main() {
// open "test.jpg"
if _, err := os.Stat("./input"); os.IsNotExist(err) {
err = os.Mkdir("input", 0777)
if err != nil {
log.Fatal(err)
}
}
if _, err := os.Stat("./output"); os.IsNotExist(err) {
err = os.Mkdir("output", 0777)
if err != nil {
log.Fatal(err)
}
}
reader := bufio.NewReader(os.Stdin)
fmt.Println("- OPTIMAZE - Image optimizer by Sharif")
fmt.Println("Put your JPG image/s in 'input' folder")
fmt.Println("width,height,quality,")
text, _ := reader.ReadString('\n')
p := strings.Split(text, ",")
w64, _ := strconv.ParseUint(string(p[0]), 10, 64)
h64, _ := strconv.ParseUint(string(p[1]), 10, 64)
q64, _ := strconv.ParseInt(string(p[2]), 10, 64)
op := p[3]
width := uint(w64)
height := uint(h64)
quality := int(q64)
files, err := ioutil.ReadDir("./input/")
if err != nil {
log.Fatal(err)
}
for _, f := range files {
//fmt.Println(f.Name())
input := "./input/"
input += f.Name()
fmt.Println(input)
imwInt, imhInt := getFileInfo(input)
imw := uint(imwInt)
imh := uint(imhInt)
switch op {
case "h":
width = imw / 2
height = imh / 2
case "q":
width = imw / 4
height = imh / 4
default:
width = imw
height = imh
}
file, err := os.Open(input)
if err != nil {
log.Fatal(err)
}
// decode jpeg into image.Image
img, err := jpeg.Decode(file)
if err != nil {
log.Fatal(err)
}
file.Close()
fmt.Println("Width:", width, "Height:", height)
var opt jpeg.Options
opt.Quality = quality
// resize to width 1000 using Lanczos resampling
// and preserve aspect ratio
m := resize.Resize(width, height, img, resize.Lanczos3)
output := "./output/"
output += f.Name()
out, err := os.Create(output)
if err != nil {
log.Fatal(err)
}
defer out.Close()
// write new image to file
jpeg.Encode(out, m, &opt)
}
}
func getFileInfo(imgPath string) (int, int) {
file, err := os.Open(imgPath)
if err != nil {
log.Fatal(err)
}
im, _, err := image.DecodeConfig(file) // Image Struct
if err != nil {
log.Fatal(err)
}
return im.Width, im.Height
}