forked from llgcode/draw2d
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileutil.go
46 lines (43 loc) · 818 Bytes
/
fileutil.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
package draw2d
import (
"bufio"
"image"
"image/png"
"os"
)
// SaveToPngFile create and save an image to a file using PNG format
func SaveToPngFile(filePath string, m image.Image) error {
// Create the file
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
// Create Writer from file
b := bufio.NewWriter(f)
// Write the image into the buffer
err = png.Encode(b, m)
if err != nil {
return err
}
err = b.Flush()
if err != nil {
return err
}
return nil
}
// LoadFromPngFile Open a png file
func LoadFromPngFile(filePath string) (image.Image, error) {
// Open file
f, err := os.OpenFile(filePath, 0, 0)
if err != nil {
return nil, err
}
defer f.Close()
b := bufio.NewReader(f)
img, err := png.Decode(b)
if err != nil {
return nil, err
}
return img, nil
}