disintegration / gift

Go Image Filtering Toolkit
MIT License
1.75k stars 121 forks source link

[Help] I am using a way to blur part of an image (not entire image), Am I correct or misusing? #28

Open mk48 opened 5 months ago

mk48 commented 5 months ago

I am using below methods to blur a face in an image. Am I using it properly or misusing?

package main

import (
    "image"
    "image/jpeg"
    "log"
    "os"

    "github.com/disintegration/gift"
)

func main() {
    faceX := 1713
    faceY := 1752
    faceWidth := 130
    faceHeight := 130

    src := loadImage("1.jpg")

    //Copy original image
    gCopyImage := gift.New()
    fullImage := image.NewRGBA(gCopyImage.Bounds(src.Bounds()))
    gCopyImage.Draw(fullImage, src)

    //Crop some part
    gCrop := gift.New(gift.Crop(image.Rect(faceX, faceY, faceX+faceWidth, faceY+faceHeight)))
    croppedImage := image.NewRGBA(gCrop.Bounds(src.Bounds()))
    gCrop.Draw(croppedImage, src)

    //Pixelate the cropped image and draw on original image
    gPixelate := gift.New(gift.Pixelate(20))
    gPixelate.DrawAt(fullImage, croppedImage, image.Point{X: faceX, Y: faceY}, gift.OverOperator)

    saveImage("out.jpg", 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)
    err = jpeg.Encode(f, img, &jpeg.Options{Quality: 99})
    if err != nil {
        log.Fatalf("png.Encode failed: %v", err)
    }
}
0xff00ff commented 3 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)
    }
}