-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
86 lines (73 loc) · 1.71 KB
/
converter.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
package main
import (
"bytes"
"fmt"
"image/png"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/chai2010/webp"
"github.com/nfnt/resize"
)
func convertAndResize(filePath string, width uint, height uint) error {
// Read the WebP file
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
// Decode the WebP image
img, err := webp.Decode(bytes.NewReader(data))
if err != nil {
return err
}
// Resize the image
resizedImg := resize.Resize(width, height, img, resize.Lanczos3)
// Save the resized image as PNG
outFile, err := os.Create(strings.TrimSuffix(filePath, ".webp") + ".png")
if err != nil {
return err
}
defer outFile.Close()
return png.Encode(outFile, resizedImg)
}
func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: ./app <directory-path> <width>x<height>")
return
}
dirPath := os.Args[1]
size := strings.Split(os.Args[2], "x")
if len(size) != 2 {
fmt.Println("Invalid size format. It should be like 32x32.")
return
}
width, err := strconv.ParseUint(size[0], 10, 32)
if err != nil {
fmt.Println("Invalid width:", err)
return
}
height, err := strconv.ParseUint(size[1], 10, 32)
if err != nil {
fmt.Println("Invalid height:", err)
return
}
// Iterate over files in the directory
files, err := ioutil.ReadDir(dirPath)
if err != nil {
fmt.Println("Error reading directory:", err)
return
}
for _, f := range files {
if filepath.Ext(f.Name()) == ".webp" {
filePath := filepath.Join(dirPath, f.Name())
err := convertAndResize(filePath, uint(width), uint(height))
if err != nil {
fmt.Printf("Failed to convert %s: %v\n", f.Name(), err)
} else {
fmt.Printf("Converted %s successfully\n", f.Name())
}
}
}
}