-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (52 loc) · 1.2 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
package main
import (
"flag"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"os"
"github.com/kolesa-team/go-webp/encoder"
"github.com/kolesa-team/go-webp/webp"
)
func main() {
inputFilePathFlag := flag.String("i", "", "Input file path")
outputFilePathFlag := flag.String("o", "", "Output file path")
qualityFlag := flag.Float64("q", 75, "Quality when converting image")
flag.Parse()
if *inputFilePathFlag == "" {
fmt.Println("Please supply an input image path!")
os.Exit(1)
}
if *outputFilePathFlag == "" {
fmt.Println("Please supply an output image path!")
os.Exit(1)
}
file, err := os.Open(*inputFilePathFlag)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
output, err := os.Create(*outputFilePathFlag)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer output.Close()
options, err := encoder.NewLossyEncoderOptions(encoder.PresetDefault, float32(*qualityFlag))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := webp.Encode(output, img, options); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("Created %s successfully\n", *outputFilePathFlag)
}