forked from esimov/caire
/
main.go
162 lines (139 loc) · 3.76 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
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/esimov/caire"
)
var (
// Flags
source = flag.String("in", "", "Source")
destination = flag.String("out", "", "Destination")
blurRadius = flag.Int("blur", 1, "Blur radius")
sobelThreshold = flag.Int("sobel", 10, "Sobel filter threshold")
newWidth = flag.Int("width", 0, "New width")
newHeight = flag.Int("height", 0, "New height")
percentage = flag.Bool("perc", false, "Reduce image by percentage")
debug = flag.Bool("debug", false, "Use debugger")
)
func main() {
flag.Parse()
if len(*source) == 0 || len(*destination) == 0 {
log.Fatal("Usage: caire -in input.jpg -out out.jpg")
}
if *newWidth > 0 || *newHeight > 0 || *percentage {
fs, err := os.Stat(*source)
if err != nil {
log.Fatalf("Unable to open source: %v", err)
}
toProcess := make(map[string]string)
p := &caire.Processor{
BlurRadius: *blurRadius,
SobelThreshold: *sobelThreshold,
NewWidth: *newWidth,
NewHeight: *newHeight,
Percentage: *percentage,
Debug: *debug,
}
switch mode := fs.Mode(); {
case mode.IsDir():
// Supported image files.
extensions := []string{".jpg", ".png", ".jpeg", ".bmp", ".gif"}
// Read source directory.
files, err := ioutil.ReadDir(*source)
if err != nil {
log.Fatalf("Unable to read dir: %v", err)
}
// Read destination file or directory.
dst, err := os.Stat(*destination)
if err != nil {
log.Fatalf("Unable to get dir stats: %v", err)
}
// Check if the image destination is a directory or a file.
if dst.Mode().IsRegular() {
log.Fatal("Please specify a directory as destination!")
os.Exit(2)
}
output, err := filepath.Abs(*destination)
if err != nil {
log.Fatalf("Unable to get absolute path: %v", err)
}
// Range over all the image files and save them into a slice.
images := []string{}
for _, f := range files {
ext := filepath.Ext(f.Name())
for _, iex := range extensions {
if ext == iex {
images = append(images, f.Name())
}
}
}
// Process images from directory.
for _, img := range images {
// Get the file base name.
name := strings.TrimSuffix(img, filepath.Ext(img))
dir := strings.TrimRight(*source, "/")
out := output + "/" + name + ".jpg"
in := dir + "/" + img
toProcess[in] = out
}
case mode.IsRegular():
toProcess[*source] = *destination
}
for in, out := range toProcess {
inFile, err := os.Open(in)
if err != nil {
log.Fatalf("Unable to open source file: %v", err)
}
defer inFile.Close()
outFile, err := os.OpenFile(out, os.O_CREATE|os.O_WRONLY, 0755)
if err != nil {
log.Fatalf("Unable to open output file: %v", err)
}
defer outFile.Close()
s := new(spinner)
s.start("Processing...")
start := time.Now()
err = p.Process(inFile, outFile)
s.stop()
if err == nil {
fmt.Printf("\nRescaled in: \x1b[92m%.2fs\n", time.Since(start).Seconds())
fmt.Printf("\x1b[39mSaved as: \x1b[92m%s \n\n", path.Base(out))
} else {
fmt.Printf("\nError rescaling image: %s. Reason: %s\n", inFile.Name(), err.Error())
}
}
} else {
log.Fatal("\x1b[31mPlease provide a width, height or percentage for image rescaling!\x1b[39m")
}
}
type spinner struct {
stopChan chan struct{}
}
// Start process
func (s *spinner) start(message string) {
s.stopChan = make(chan struct{}, 1)
go func() {
for {
for _, r := range `-\|/` {
select {
case <-s.stopChan:
return
default:
fmt.Printf("\r%s%s %c%s", message, "\x1b[92m", r, "\x1b[39m")
time.Sleep(time.Millisecond * 100)
}
}
}
}()
}
// End process
func (s *spinner) stop() {
s.stopChan <- struct{}{}
}