2020-05-10 15:40:56 +00:00
|
|
|
package img
|
2020-05-10 07:11:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
|
|
|
"image/png"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2020-05-10 15:40:56 +00:00
|
|
|
func EncodePNG(path string, im image.Image) error {
|
2020-05-10 07:11:08 +00:00
|
|
|
dst, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
return png.Encode(dst, im)
|
|
|
|
}
|
|
|
|
|
2020-05-10 15:40:56 +00:00
|
|
|
func DecodeImage(path string) (image.Image, error) {
|
2020-05-10 07:11:08 +00:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
im, _, err := image.Decode(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return im, nil
|
|
|
|
}
|