-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgodotify.go
68 lines (56 loc) · 1.28 KB
/
godotify.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
package godotify
import (
"image"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"strings"
"github.com/disintegration/imaging"
)
type Config struct {
Intensity float64
}
func GoDotify(inputFile string, outputFile string, config Config) error {
//openfile
infile, err := os.Open(inputFile)
if err != nil {
return err
}
defer infile.Close()
//decode
src, err := imaging.Decode(infile)
if err != nil {
return err
}
//to make it easier for humans (... reverse intensity val 0 - 1)
intensity := 1.1 - config.Intensity
if intensity < 0 {
intensity = 0
} else if intensity > 1 {
intensity = 1
}
//make img smaller
smaller := imaging.Resize(src, int(float64(src.Bounds().Dx())*0.1*intensity), 0, imaging.Lanczos)
//make img bigger again
godotified := imaging.Resize(smaller, src.Bounds().Dx(), src.Bounds().Dy(), imaging.NearestNeighbor)
//openfile (output)
outfile, err := os.Create(outputFile)
if err != nil {
return err
}
defer outfile.Close()
//encode and write output file
ext := strings.ToLower(filepath.Ext(outputFile))
if ext == ".png" {
err = png.Encode(outfile, godotified)
} else if ext == ".jpg" || ext == ".jpeg" {
err = jpeg.Encode(outfile, godotified, nil)
} else {
return image.ErrFormat
}
if err != nil {
return err
}
return nil
}