Merge pull request #60 from emaele/jpeg-open&save

Load and save jpeg images
This commit is contained in:
Michael Fogleman 2019-01-19 16:22:23 -05:00 committed by GitHub
commit 3795562800
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

23
util.go
View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"image" "image"
"image/draw" "image/draw"
"image/jpeg"
_ "image/jpeg" _ "image/jpeg"
"image/png" "image/png"
"io/ioutil" "io/ioutil"
@ -53,6 +54,28 @@ func SavePNG(path string, im image.Image) error {
return png.Encode(file, im) return png.Encode(file, im)
} }
func LoadJPG(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return jpeg.Decode(file)
}
func SaveJPG(path string, im image.Image, quality int) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
var opt jpeg.Options
opt.Quality = quality
return jpeg.Encode(file, im, &opt)
}
func imageToRGBA(src image.Image) *image.RGBA { func imageToRGBA(src image.Image) *image.RGBA {
bounds := src.Bounds() bounds := src.Bounds()
dst := image.NewRGBA(bounds) dst := image.NewRGBA(bounds)