image manipulation with GIFT
This commit is contained in:
parent
7352e9e999
commit
bb3a2d2de3
|
@ -0,0 +1,58 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/disintegration/gift"
|
||||
"image"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
_ "image/jpeg"
|
||||
"image/png"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Colorize [3]float32
|
||||
Contrast int
|
||||
Gamma float64
|
||||
Root string
|
||||
}
|
||||
|
||||
func loadImage(filename string) image.Image {
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
log.Fatalf("os.Open failed: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
img, _, err := image.Decode(f)
|
||||
if err != nil {
|
||||
log.Fatalf("image.Decode failed: %v", err)
|
||||
}
|
||||
return img
|
||||
}
|
||||
|
||||
func saveImage(filename string, img image.Image) {
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
log.Fatalf("os.Create failed: %v", err)
|
||||
}
|
||||
defer f.Close()
|
||||
err = png.Encode(f, img)
|
||||
if err != nil {
|
||||
log.Fatalf("png.Encode failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func processImage(img image.Image, config Config) image.Image {
|
||||
|
||||
cArgs := config.Colorize
|
||||
g := gift.New(
|
||||
gift.Colorize(cArgs[0], cArgs[1], cArgs[2]),
|
||||
gift.Contrast(float32(config.Contrast)),
|
||||
gift.Gamma(float32(config.Gamma)),
|
||||
)
|
||||
|
||||
dst := image.NewRGBA(g.Bounds(img.Bounds()))
|
||||
g.Draw(dst, img)
|
||||
|
||||
return dst
|
||||
}
|
Loading…
Reference in New Issue