-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresizr.go
187 lines (165 loc) · 4.02 KB
/
resizr.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"bufio"
"errors"
"fmt"
"github.com/nfnt/resize"
"github.com/urfave/cli"
"image"
"image/jpeg"
"log"
"os"
"path/filepath"
"strings"
)
func max(x, y int) int {
if x > y {
return x
}
return y
}
func checkIfTargetImageExists(outName string, size int) (bool, error) {
//File exists?
if _, err := os.Stat(outName); err != nil {
return false, nil
}
//File can be opened?
file, err := os.Open(outName)
defer file.Close()
if err != nil {
return false, err
}
//File can be decoded?
img, err := jpeg.Decode(file)
if err != nil {
return false, err
}
//Size is already ok?
maxsize := max(img.Bounds().Size().X, img.Bounds().Size().Y)
return maxsize == size, nil
}
func resizeJpeg(inName, outName string, size int) error {
file, err := os.Open(inName)
defer file.Close()
if err != nil {
return err
}
img, err := jpeg.Decode(file)
if err != nil {
return err
}
var m image.Image
if img.Bounds().Size().X > img.Bounds().Size().Y {
m = resize.Resize(uint(size), 0, img, resize.Lanczos3)
} else {
m = resize.Resize(0, uint(size), img, resize.Lanczos3)
}
out, err := os.Create(outName)
defer out.Close()
if err != nil {
return err
}
jpeg.Encode(out, m, nil)
return nil
}
func createPathToFile(resizepath string) error {
folderPath := filepath.Dir(resizepath)
return os.MkdirAll(folderPath, os.ModePerm) // Returns an error (or nil)
}
func printOperation(origpath, resizepath string) {
log.Printf("Operation: %s -> %s \n", origpath, resizepath)
}
func resizeOperation(origpath, resizepath string, size int) error {
err := createPathToFile(resizepath)
if err != nil {
return err
}
if exists, _ := checkIfTargetImageExists(resizepath, size); exists == true {
return errors.New("Resized image exists. Skipping.")
}
err = resizeJpeg(origpath, resizepath, size)
if err != nil {
return err
}
return nil
}
var operationCount int = 0
func newVisitFunc(operation func(string, string, int) error, origRoot, resizeRoot string, size int) func(string, os.FileInfo, error) error {
return func(path string, f os.FileInfo, err error) error {
if f.IsDir() {
return nil
}
if err != nil { // passed from walk
return err
}
ext := strings.ToLower(string(filepath.Ext(path)))
if ext == ".jpeg" || ext == ".jpg" {
relativPath, _ := filepath.Rel(origRoot, path)
resizepath := filepath.Join(resizeRoot, relativPath)
errop := operation(path, resizepath, size)
if errop != nil {
printOperation(path, resizepath)
log.Printf("Error: %q", errop.Error())
return nil // Skip error!
}
operationCount++
fmt.Printf("Converted: %d images.\r", operationCount)
}
return nil
}
}
func ResizeTree(origRoot, resizeRoot string, size int) {
visit := newVisitFunc(resizeOperation, origRoot, resizeRoot, size)
filepath.Walk(origRoot, visit)
}
func askUserToContinue() bool {
fmt.Printf("Continue? [yN]: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
if scanner.Err() != nil || scanner.Text() != "y" {
return false
}
return true
}
func mainCommand(c *cli.Context) error {
source := c.Args().Get(0)
dest := c.String("dest")
size := c.Int("size")
if source == "" {
log.Fatal("Source must be given.")
}
if _, err := os.Stat(source); err != nil {
log.Fatal(err)
}
log.Printf("Converting: %q to %q with size %d. \n", source, dest, size)
if !c.Bool("no-ask") && !askUserToContinue() {
log.Printf("Cancelled by user.")
return nil
}
ResizeTree(source, dest, size)
return nil
}
func main() {
app := cli.NewApp()
app.Name = "resizr"
app.Usage = "Create small image previews in seperate folder structure\n\n Example: resizr --dest /home/user/preview --size 1024 /home/user/pictures"
app.Version = "0.1"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "dest, o",
Value: ".",
Usage: "Destination directory",
},
cli.IntFlag{
Name: "size, s",
Value: 1024,
Usage: "Set default max image width/height",
},
cli.BoolFlag{
Name: "no-ask, y",
Usage: "Skip question if we should continue.",
},
}
app.Action = mainCommand
app.Run(os.Args)
}