-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.go
53 lines (42 loc) · 1.01 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
package main
import (
"bytes"
"image"
"log"
"math"
"github.com/disintegration/imaging"
)
const (
threshold = .45
dimLevel = -10
compressLevel = 90
)
// calculate brightness using RMS of grayscaled picture
func checkDarkImage(img image.Image) (bool, error) {
newImage := imaging.Grayscale(img)
var sum int
for i, v := range newImage.Pix {
if i%4 == 0 {
sum += int(v) * int(v)
}
}
a := sum / (newImage.Rect.Max.X * newImage.Rect.Max.Y)
result := math.Sqrt(float64(a))
darkScale := result / float64(255)
return darkScale < threshold, nil
}
func dimImage(img image.Image) image.Image {
return imaging.AdjustBrightness(img, dimLevel)
}
func getDimensions(img image.Image) (int, int, error) {
return img.Bounds().Dx(), img.Bounds().Dy(), nil
}
func encodePNG(img image.Image) ([]byte, error) {
encodeOption := imaging.JPEGQuality(compressLevel)
var buf bytes.Buffer
err := imaging.Encode(&buf, img, imaging.JPEG, encodeOption)
if err != nil {
log.Fatal(err)
}
return buf.Bytes(), nil
}