-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_utils.go
102 lines (87 loc) · 2.78 KB
/
image_utils.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
package lblconv
import (
"image"
"image/jpeg"
"image/png"
"math"
"os"
"path/filepath"
"strings"
"github.com/disintegration/imaging"
)
// resizeImage resamples the image to match the longer and shorter sides (one may be 0) and writes
// the output to outPath.
//
// Returns the resized image along with the width and height scale factors.
func resizeImage(img image.Image, longerSide, shorterSide int,
downsamplingFilter, upsamplingFilter imaging.ResampleFilter) (
resized image.Image, scaleWidth, scaleHeight float64, err error) {
imgBounds := img.Bounds()
imgWidth := imgBounds.Dx()
imgHeight := imgBounds.Dy()
imgLonger := imgWidth
imgShorter := imgHeight
isLandscape := true
if imgHeight > imgWidth {
imgLonger = imgHeight
imgShorter = imgWidth
isLandscape = false
}
// Calculate the target dimensions.
if longerSide <= 0 {
longerSide = int(math.Round(float64(shorterSide) * (float64(imgLonger) / float64(imgShorter))))
} else if shorterSide <= 0 {
shorterSide = int(math.Round(float64(longerSide) * (float64(imgShorter) / float64(imgLonger))))
}
// Select the filter based on the direction of the rescaling operation.
var filter imaging.ResampleFilter
if longerSide*shorterSide < imgWidth*imgHeight {
filter = downsamplingFilter
} else {
filter = upsamplingFilter
}
// Resize.
if isLandscape {
resized = imaging.Resize(img, longerSide, shorterSide, filter)
scaleWidth = float64(longerSide) / float64(imgLonger)
scaleHeight = float64(shorterSide) / float64(imgShorter)
} else { // Portrait.
resized = imaging.Resize(img, shorterSide, longerSide, filter)
scaleWidth = float64(shorterSide) / float64(imgShorter)
scaleHeight = float64(longerSide) / float64(imgLonger)
}
return resized, scaleWidth, scaleHeight, nil
}
// decodeImageConfig opens the file at path and returns the results of image.DecodeConfig.
func decodeImageConfig(path string) (config image.Config, format string, err error) {
f, err := os.Open(path)
if err != nil {
return image.Config{}, "", err
}
defer closeWithErrCheck(f, &err)
return image.DecodeConfig(f)
}
// loadImage reads and decodes the image at path and returns the results of image.Decode.
func loadImage(path string) (img image.Image, format string, err error) {
f, err := os.Open(path)
if err != nil {
return nil, "", err
}
defer closeWithErrCheck(f, &err)
return image.Decode(f)
}
// Saves the image to path, encoding it as PNG or JPG, depending on the file extension of path.
func saveImage(path string, img image.Image, jpegQuality int) (err error) {
f, err := os.Create(path)
if err != nil {
return err
}
defer closeWithErrCheck(f, &err)
switch strings.ToLower(filepath.Ext(path)) {
case ".png":
err = png.Encode(f, img)
default:
err = jpeg.Encode(f, img, &jpeg.Options{Quality: jpegQuality})
}
return err
}