-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
57 lines (48 loc) · 1014 Bytes
/
utils.go
File metadata and controls
57 lines (48 loc) · 1014 Bytes
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
package main
import (
"fmt"
"image"
"image/color"
"image/jpeg"
"image/png"
"os"
"sync"
"manas140/seam/out"
)
func col(text string, col int) string {
return fmt.Sprintf("\033[0;%dm%s\033[1;0m", col, text)
}
func toGray(c color.Color) int {
r, g, b, _ := c.RGBA()
return int((0.299*float64(r) + 0.587*float64(g) + 0.114*float64(b)) / 256)
}
func dirExists(dir string) bool {
if _, err := os.Open(dir); err == nil {
return true
} else {
return false
}
}
func readImage(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
return img, nil
}
func saveImage(img image.Image, path string, ext string, quality int, wg *sync.WaitGroup) {
file, err := os.Create(path)
if err != nil {
out.Error("Unable to create file '%s'", path)
}
defer file.Close()
switch ext {
case "png":
png.Encode(file, img)
default:
jpeg.Encode(file, img, &jpeg.Options{Quality: quality})
}
wg.Done()
}