disintegration / imaging

Imaging is a simple image processing package for Go
MIT License
5.28k stars 440 forks source link

cropping bounding box #116

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hi,

Hope you are all well !

I am working on a fork of https://github.com/LdDl/go-darknet and I need to crop images inside the bounding box detected.

So, is it possible to crop an image with bounding box parameters with imaging ?

For example:

Thanks in advance for your input and insights on that.

Cheers, X

disintegration commented 4 years ago

Hi,

Is this what you need?

package main

import (
    "image"
    "log"

    "github.com/disintegration/imaging"
)

func main() {
    img, err := imaging.Open("FL-823-GFB.jpg")
    if err != nil {
        log.Fatal(err)
    }

    var (
        x = 42
        y = 51
        w = 772
        h = 485
    )

    img = imaging.Crop(img, image.Rect(x, y, x+w, y+h))

    if err := imaging.Save(img, "result.jpg"); err != nil {
        log.Fatal(err)
    }
}
ghost commented 4 years ago

yeah, we have implemented that https://github.com/LdDl/go-darknet/blob/master/example/main.go#L84.

Thanks a lot !