Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mk48 opened this issue May 27, 2024 · 1 comment

Comments

@mk48
Copy link

mk48 commented May 27, 2024

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
Copy link

@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)
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants