/
image.go
231 lines (209 loc) · 6.53 KB
/
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package utils
import (
"fmt"
"github.com/fogleman/gg"
"image"
"image/color"
"image/draw"
"image/jpeg"
_ "image/jpeg"
_ "image/png"
"os"
"path/filepath"
"sync"
"time"
)
func IsDir(path string) bool {
fileInfo, err := os.Stat(path)
if err != nil {
//fmt.Printf("Invalid file path at %s: %v\n", path, err)
//fmt.Printf("Please check if the file exists\n")
return false
}
return fileInfo.IsDir()
}
// TDDO: Image validation
func readImage(path string) (image.Image, error) {
img, err := os.Open(path)
if err != nil {
fmt.Printf("Failed to read image at %s: %v\n", path, err)
return nil, err
}
defer img.Close()
photo, _, err := image.Decode(img)
if err != nil {
fmt.Printf("Failed to load image: %v\n", err)
return nil, err
}
return photo, nil
}
func saveImage(path string, photo image.Image) error {
outIMG, err := os.Create(path)
if err != nil {
fmt.Printf("Failed to save image at %s: %v\n", path, err)
return err
}
defer outIMG.Close()
err = jpeg.Encode(outIMG, photo, nil)
if err != nil {
fmt.Printf("Failed to convert image format: %v\n", err)
return err
}
return nil
}
func createCanvas(photo image.Image, borderRatio float64, squared bool, c, fontColor color.RGBA, signature string, fontSize int) (canvas *image.RGBA) {
// Determine the size of the border and the position of the photo
width, height := photo.Bounds().Dx(), photo.Bounds().Dy()
var startX, startY int
// Squared or original ratio
if squared {
borderWidth := int(borderRatio * float64(min(width, height)))
squareSize := max(width, height) + 2*borderWidth
// Create a squared RGBA canvas
canvas = image.NewRGBA(image.Rect(0, 0, squareSize, squareSize))
// Calculate the starting point
startX = (squareSize - width) / 2
startY = (squareSize - height) / 2
} else {
borderWidth := int(borderRatio * float64(width))
borderHeight := int(borderRatio * float64(height))
// Create a squared RGBA canvas
canvas = image.NewRGBA(image.Rect(0, 0,
width+2*borderWidth, height+2*borderHeight))
// Calculate the starting point
startX = borderWidth
startY = borderHeight
}
// Fill the entire canvas with frameColor (default color is white)
draw.Draw(canvas, canvas.Bounds(),
&image.Uniform{c}, image.Point{}, draw.Over)
// Place the photo at the center of the canvas
draw.Draw(canvas, image.Rect(startX, startY, startX+width, startY+height),
photo, image.Point{}, draw.Over)
if signature != "" {
// Sign the framed image
dc := gg.NewContextForRGBA(canvas)
dc.SetColor(fontColor)
// Default font: Inter-Regular
dc.LoadFontFace("font.ttf", float64(fontSize))
// Determine the signature position
// TODO: Make the signature position customizable
textWidth, _ := dc.MeasureString(signature)
dc.DrawString(signature, (float64(canvas.Bounds().Dx())-textWidth)/2, float64(canvas.Bounds().Dy()-fontSize))
}
return
}
func AddFrames(sourcePath, outPath string, borderRatio float64, squared bool, c, fontColor color.RGBA, signature string, fontSize int) error {
if outPath == "" || IsDir(outPath) {
currentTime := time.Now()
outPath += currentTime.Format("2112_01_02_03_04_05")
}
// Determine if the source is a directory or a file
// If it's a directory, create a new directory to contain all the framed images
if IsDir(sourcePath) {
err := os.Mkdir(outPath, os.ModePerm)
if err != nil {
fmt.Printf("Failed to create a new directory at %s: %v\n", outPath, err)
return err
}
images, err := os.ReadDir(sourcePath)
if err != nil {
fmt.Printf("Failed to read images from %s: %v\n", sourcePath, err)
return err
}
var wg sync.WaitGroup
for _, file := range images {
// Ignore subdirectories and .DS_Store files
if file.IsDir() || filepath.Ext(file.Name()) == ".DS_Store" {
continue
}
wg.Add(1)
go func(imgPath, savePath string) {
defer wg.Done()
photo, err := readImage(imgPath)
if err != nil {
fmt.Printf("Failed to read image at %s: %v\n", imgPath, err)
return
}
canvas := createCanvas(photo, borderRatio, squared, c, fontColor, signature, fontSize)
err = saveImage(savePath, canvas)
if err != nil {
fmt.Printf("Failed to save image at %s: %v\n", savePath, err)
return
}
fmt.Printf("Image successfully saved to: %s \u2713\n", savePath)
}(filepath.Join(sourcePath, file.Name()), filepath.Join(outPath, file.Name()))
}
wg.Wait() // Wait for all goroutines to finish
} else {
// Just one image
format := filepath.Ext(sourcePath)
outPath = outPath + format
photo, err := readImage(sourcePath)
if err != nil {
fmt.Printf("Failed to read image at %s: %v", sourcePath, err)
return err
}
canvas := createCanvas(photo, borderRatio, squared, c, fontColor, signature, fontSize)
err = saveImage(outPath, canvas)
if err != nil {
fmt.Printf("Failed to save image at %s: %v", outPath, err)
return err
}
fmt.Printf("Image successfully saved to: %s \u2713\n", outPath)
}
return nil
}
func Cut(sourcePath, grid string) error {
photo, err := readImage(sourcePath)
if err != nil {
fmt.Printf("Failed to read image at %s: %v", sourcePath, err)
return err
}
// Split the image into a grid
width, height := photo.Bounds().Dx(), photo.Bounds().Dy()
err = os.Mkdir("GRID", os.ModePerm)
if err != nil {
fmt.Printf("Failed to create directory: %v", err)
fmt.Printf("Please remove the existing directory 'GRID' and try again\n")
return err
}
if grid == "4" {
// Split the image into 4 parts
for i := 0; i < 2; i++ {
for j := 0; j < 2; j++ {
// Create a new RGBA canvas
canvas := image.NewRGBA(image.Rect(0, 0, width/2, height/2))
// Obtain each part of the image
draw.Draw(canvas, image.Rect(0, 0, width/2, height/2),
photo, image.Point{width / 2 * i, height / 2 * j}, draw.Over)
// Save the image
err = saveImage(fmt.Sprintf("GRID/out_%d_%d.jpg", i, j), canvas)
if err != nil {
fmt.Printf("Failed to save image: %v", err)
return err
}
}
}
} else if grid == "9" {
// Split the image into 9 parts
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
// Create a new RGBA canvas
canvas := image.NewRGBA(image.Rect(0, 0, width/3, height/3))
// Obtain each part of the image
draw.Draw(canvas, image.Rect(0, 0, width/3, height/3),
photo, image.Point{width / 3 * i, height / 3 * j}, draw.Over)
// Save the image
err = saveImage(fmt.Sprintf("GRID/out_%d_%d.jpg", i, j), canvas)
if err != nil {
fmt.Printf("Failed to save image: %v", err)
return err
}
}
}
} else {
return fmt.Errorf("Invalid grid option.. \nPlease use 4 or 9..")
}
return nil
}