-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprocess_image.go
164 lines (132 loc) · 3.49 KB
/
process_image.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
// This file is designed to process subtitles
package main
import (
"fmt"
"image"
"image/draw"
"image/jpeg"
"io/ioutil"
"log"
"os"
"strconv"
"github.com/BurntSushi/toml"
)
type File struct {
name *image.Image
horizontalrate int
verticalrate int
}
type Config struct {
ImageDirectory string
HorizontalRate int
VerticalRate int
OutputFilename string
}
var config Config
func init() {
readConfig()
}
func readConfig() {
configfile := "config.toml"
if _, err := toml.DecodeFile(configfile, &config); err != nil {
log.Fatal(err)
}
}
func generatenewImage(files []File) {
fmt.Printf("There are %d pictures that will need to be processed\n", len(files))
// Build a white picture to store all parts
// draw.Draw(dst, r, src, sp, op)
// calculate length first , then width
AllWidth := (*(files[0]).name).Bounds().Dx()
height := (*(files[0]).name).Bounds().Dy()
AllHeight := height * len(files)
fmt.Println(AllWidth)
target := image.Rectangle{image.Point{0, 0}, image.Point{AllWidth, AllHeight}}
rgba := image.NewRGBA(target)
var tempRectangle image.Rectangle
for index := 0; index < len(files); index++ {
sp := image.Point{0, height * index}
/**
The coordinate of first picture is (0,0)
The second picture will be (0, weight of width)
refer : https://blog.golang.org/go-imagedraw-package
**/
tempRectangle = image.Rectangle{sp, sp.Add((*files[index].name).Bounds().Size())}
draw.Draw(rgba, tempRectangle, *files[index].name,
image.Point{0, 0}, draw.Src)
}
out, err := os.Create(config.OutputFilename)
defer out.Close()
if err != nil {
fmt.Println(err)
}
jpeg.Encode(out, rgba, nil)
}
func subImage(fileName string, verticalrate int, horizontalrate int) (output image.Image) {
fmt.Println(fileName)
reader, err := os.Open(fileName)
if err != nil {
fmt.Println(err)
}
defer reader.Close()
m, _, err := image.Decode(reader)
if err != nil {
log.Fatal(err)
}
width, height := m.Bounds().Dx(), m.Bounds().Dy()
// TODO variabledivision is confusing
absoluteHorizontalLength := (height * horizontalrate) / 10.0
x := m.(interface {
SubImage(r image.Rectangle) image.Image
}).SubImage(image.Rect(0, height-absoluteHorizontalLength, width, height))
fmt.Println(x.Bounds().Size())
return x
}
func getFileNumber(dir string) int {
files, _ := ioutil.ReadDir(dir)
return len(files)
}
func saveImageToFile(input image.Image, name string) {
outf, err := os.Create(name)
if err != nil {
fmt.Println(err)
}
jpeg.Encode(outf, input, nil)
}
func openImage(fileName string) (target image.Image) {
reader, err := os.Open(fileName)
if err != nil {
fmt.Println(err)
}
defer reader.Close()
m, _, err := image.Decode(reader)
if err != nil {
log.Fatal(err)
}
return m
}
func main() {
config.HorizontalRate = 2
s := make([]File, 0)
fileNumber := getFileNumber(config.ImageDirectory)
for index := 0; index < fileNumber; index++ {
// string() tricky...
filename := config.ImageDirectory + string(os.PathSeparator) + strconv.Itoa(index+1) + ".JPG"
// crop image
sideProduct := subImage(filename, config.VerticalRate, config.HorizontalRate)
// save to file
saveImageToFile(sideProduct, fmt.Sprintf("%d_middle.jpg", index))
// reopen to get Image Object
target := openImage(fmt.Sprintf("%d_middle.jpg", index))
// add to struct
file := File{
name: &target,
horizontalrate: 2,
verticalrate: 0,
}
// add struct to slice
s = append(s, file)
}
generatenewImage(s)
fmt.Println("the joint picture is generated")
}