From 74a8429cb884252e7c07677986aeaf9afe2da3fe Mon Sep 17 00:00:00 2001 From: Emanuele Rocco Petrone Date: Sat, 19 Jan 2019 18:47:38 +0100 Subject: [PATCH] load and save jpeg images --- util.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/util.go b/util.go index 940517d..0806235 100644 --- a/util.go +++ b/util.go @@ -4,6 +4,7 @@ import ( "fmt" "image" "image/draw" + "image/jpeg" _ "image/jpeg" "image/png" "io/ioutil" @@ -53,6 +54,28 @@ func SavePNG(path string, im image.Image) error { 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 { bounds := src.Bounds() dst := image.NewRGBA(bounds)