/
blur.go
157 lines (132 loc) · 3.44 KB
/
blur.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
/*
*
* Please see README.md for more information
*
* Usage: blur
* Description: Blurr's the files in the "orig" directory and outputs to an "auto-dest" directory
*
*/
package main
import (
"fmt"
"github.com/disintegration/imaging"
"image"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
func main() {
//
// Define the factors we want for the blurrings
//
// factors := [...]string{"0.5", "0.25", "1"}
//
//
// map from FactorMultiple to FactorName
factors := map[string]string{
"0": "",
"3": "-blur3",
"6": "-blur6",
"9": "-blur9",
"12": "-blur12",
"15": "-blur15",
"18": "-blur18",
"21": "-blur21",
"24": "-blur24",
"27": "-blur27",
"30": "-blur30",
}
//
// clean auto-dest directory
//
err := os.RemoveAll("auto-dest")
errorCheck(err)
//
// foreach factor
//
for factor, factorName := range factors {
//
// convert the factor string to a float64
//
factorFloat, err := strconv.ParseFloat(factor, 64)
errorCheck(err)
//
// Create the factor destination directory
//
fmt.Println("Factoring for", factorName, "...blur", factor)
destPath := fmt.Sprintf("auto-dest")
err = os.MkdirAll(destPath, 0777)
errorCheck(err)
//
// foreach file in the original directory, resize it!
//
files, err := ioutil.ReadDir("orig")
errorCheck(err)
for _, file := range files {
origFilename := file.Name() // eg. surfboard.png
origFilenameWithDir := fmt.Sprintf("orig/%s", origFilename) // eg. "orig/surfboard.png"
//
// Work out what our output filename should be
//
origFilenameFilepathBase := filepath.Base(origFilename)
origFilenameExt := filepath.Ext(origFilenameFilepathBase) // eg. ".png"
origFilenameBasename := origFilenameFilepathBase[:len(origFilenameFilepathBase)-len(origFilenameExt)] // eg. "monk"
destFilenameWithDir := fmt.Sprintf("%s/%s%s%s", destPath, origFilenameBasename, factorName, origFilenameExt)
//
// Detect filetype and process...
//
if (strings.HasSuffix(origFilename, ".png")) {
blurImage(origFilenameWithDir, destFilenameWithDir, factorFloat, imaging.PNG)
} else if (strings.HasSuffix(origFilename, ".jpg")) {
blurImage(origFilenameWithDir, destFilenameWithDir, factorFloat, imaging.JPEG)
}else {
fmt.Println("Not processing", origFilenameWithDir, "as it's type could not be detected")
}
}
}
// blurImage("orig/monk.png", "result.png", float64(3.5))
}
/*
blurImage: blur the image and save it
*/
func blurImage(inFilename string, outFilename string, factor float64, format imaging.Format) {
//
// print a message about what we are converting
//
fmt.Println("\t\t", inFilename, "=>", outFilename, "using factor", factor)
//
// Open the file and read in the image
//
infile, err := os.Open(inFilename)
errorCheck(err)
defer infile.Close()
//
// load in the actual image data (decode image) and work out the new dimensions
//
srcImage, _, err := image.Decode(infile)
errorCheck(err)
//
// blue the image...
//
blurredImage := imaging.Blur(srcImage, factor)
//
// save the new image
//
outfile, err := os.Create(outFilename)
errorCheck(err)
defer outfile.Close()
imaging.Encode(outfile, blurredImage, format)
// png.Encode(outfile, blurredImage)
}
/*
errorCheck: Helper function to check for errors and log/panic on fail
*/
func errorCheck(e error) {
if e != nil {
log.Fatalf("%s", e)
panic(e)
}
}