Open mk48 opened 5 months ago
@mk48 Here is an example of how to draw an image over another one.
package main
import (
"image"
"image/png"
"log"
"os"
"github.com/disintegration/gift"
)
func main() {
faceX := 10
faceY := 10
faceWidth := 10
faceHeight := 10
src := loadImage("input.png")
//small image to cover the face
gCopyImage := gift.New()
gsrc := image.NewGray(image.Rectangle{ // NewGray it's just an example to see better the result
Min: image.Point{X: faceX, Y: faceY},
Max: image.Point{X: faceX + faceWidth, Y: faceY + faceHeight},
})
gCopyImage.Draw(gsrc, src)
// original image where to paste the small image
fullImage := image.NewRGBA(gCopyImage.Bounds(src.Bounds()))
gCopyImage.Draw(fullImage, src)
// paste the small image on the original image
gCopyImage.DrawAt(fullImage, gsrc, image.Point{X: faceX, Y: faceY}, gift.OverOperator)
saveImage("output.png", fullImage)
}
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)
}
}
I am using below methods to blur a face in an image. Am I using it properly or misusing?