-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathopencv.go
71 lines (62 loc) · 1.42 KB
/
opencv.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
// +build opencv all
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/daddye/trez"
"github.com/lazywei/go-opencv/opencv"
)
func init() {
RegisterResizer("opencv", resizeOpenCv)
RegisterResizer("T-REZ", resizeTRez)
}
func resizeOpenCv(origName, newName string) (int, int64) {
iplImg := opencv.LoadImage(origName)
if iplImg == nil {
panic("LoadImage fail")
}
defer iplImg.Release()
resizedIplImg := opencv.Resize(iplImg, 150, 0, 0)
opencv.SaveImage(newName, resizedIplImg, 0)
origFileStat, _ := os.Stat(origName)
newFileStat, _ := os.Stat(newName)
return int(newFileStat.Size()), origFileStat.Size()
}
func resizeTRez(origName, newName string) (int, int64) {
origFileStat, _ := os.Stat(origName)
options := trez.Options{
Width: 150,
Height: 150,
Algo: trez.FIT,
Quality: 95,
}
origFile, err := os.Open(origName)
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
defer origFile.Close()
buf, err := ioutil.ReadAll(origFile)
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
buf, err = trez.Resize(buf, options)
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
cacheFile, err := os.Create(newName)
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
defer cacheFile.Close()
_, err = cacheFile.Write(buf)
if err != nil {
fmt.Println(err)
return 0, origFileStat.Size()
}
return int(len(buf)), origFileStat.Size()
}