This repository has been archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimageCombine.go
176 lines (153 loc) · 4.45 KB
/
imageCombine.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
package main
import (
"encoding/json"
"flag"
"github.com/fogleman/gg"
"github.com/nfnt/resize"
"github.com/panjf2000/ants"
"image"
"image/draw"
"image/jpeg"
"io/ioutil"
"log"
"os"
"path"
"runtime"
"runtime/pprof"
"strconv"
"time"
)
type InputJsonData struct {
Width int `json:"width"`
Height int `json:"height"`
Format string `json:"format"`
FrameNum []int `json:"timestamps"`
}
type ImageWithPosition struct {
img image.Image
row int
column int
}
// error handler
func check(err error) {
if err != nil {
panic(err)
}
}
func generateFinImage(thumbnailWidth, thumbnailHeight, columnLength, rowWidth int) *image.RGBA {
finRect := image.Rectangle{
Min: image.ZP,
Max: image.Point{X: thumbnailWidth * rowWidth, Y: thumbnailHeight * columnLength},
}
return image.NewRGBA(finRect)
}
func resizeImg(img *image.Image, thumbnailSize uint) *image.Image {
rezImg := resize.Thumbnail(thumbnailSize, thumbnailSize, *img, resize.NearestNeighbor) // pillow use NEAREST filter as default
return &rezImg
}
func getDataFromJsonFile(path string) *InputJsonData {
jsonData, err := ioutil.ReadFile(path)
check(err)
var data InputJsonData
err = json.Unmarshal([]byte(jsonData), &data)
if err != nil {
panic(err)
}
return &data
}
func divmod(numerator, denominator int) (quotient, remainder int) {
quotient, remainder = numerator/denominator, numerator%denominator
return
}
func getThumbnailWidthHeight(imgPath string, thumbnailSize uint) (thumbnailWidth, thumbnailHeight int) {
img, err := gg.LoadJPG(imgPath)
check(err)
img = *resizeImg(&img, thumbnailSize)
thumbnailWidth = img.Bounds().Dx()
thumbnailHeight = img.Bounds().Dy()
return
}
func handleSingleImage(imgPath string, thumbnailSize uint, row, column int) (imgp *ImageWithPosition) {
img, err := gg.LoadJPG(imgPath)
check(err)
img = *resizeImg(&img, thumbnailSize)
thumbnailWidth := img.Bounds().Dx()
thumbnailHeight := img.Bounds().Dy()
imgp = &ImageWithPosition{img, row * thumbnailWidth, column * thumbnailHeight}
return
}
type param struct {
imgPath string
thumbnailSize uint
row, column int
}
func generateJPG(imageFolderPath, jsonPath string, sampleInterval, columnLength int, thumbnailSize uint) *image.RGBA {
jsonData := getDataFromJsonFile(jsonPath)
frames := jsonData.FrameNum
imgNum := len(frames)
thumbnailWidth, thumbnailHeight := getThumbnailWidthHeight(path.Join(imageFolderPath, "0.jpeg"), thumbnailSize)
row, column := 0, 0
c := make(chan *ImageWithPosition, imgNum/sampleInterval)
p, _ := ants.NewPoolWithFunc(1000, func(payload interface{}) {
params, ok := payload.(*param)
if !ok {
return
}
res := func(params *param) *ImageWithPosition {
return handleSingleImage(params.imgPath, params.thumbnailSize, params.row, params.column)
}(params)
c <- res
})
defer p.Release()
for i := 0; i <= imgNum-sampleInterval; i += sampleInterval {
row, column = divmod(i/sampleInterval, columnLength)
params := ¶m{path.Join(imageFolderPath, strconv.Itoa(i)+".jpeg"), thumbnailSize,
row, column}
_ = p.Invoke(params)
}
finImage := generateFinImage(thumbnailWidth, thumbnailHeight, columnLength, row+1)
for i := 0; i < imgNum/sampleInterval; i++ {
p := <-c
rect := image.Rectangle{Min: image.Point{X: p.row, Y: p.column}, Max: image.Point{X: p.row, Y: p.column}.Add(p.img.Bounds().Size())}
draw.Draw(finImage, rect, p.img, image.ZP, draw.Src)
}
return finImage
}
var cpuprofile = flag.String("cpuprofile", "cpu.prof", "write cpu profile to file.")
var memprofile = flag.String("memprofile", "mem.prof", "write memory profile to file")
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
start := time.Now()
finImage := generateJPG("test_files",
"test.json",
20,
25,
100)
out, err := os.Create("./JPG.jpg")
check(err)
err = jpeg.Encode(out, finImage, &jpeg.Options{Quality: 75})
check(err)
print("time consumption: ", time.Now().Sub(start).Seconds(), "\n")
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close()
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
}