smartcrop finds good image crops for arbitrary sizes. It is a pure Go implementation, based on Jonas Wagner's smartcrop.js
Image: https://www.flickr.com/photos/usfwspacific/8182486789 by Washington Dept of Fish and Wildlife, originally licensed under CC-BY-2.0 when the image was imported back in September 2014
Image: https://www.flickr.com/photos/endogamia/5682480447 by Leon F. Cabeiro (N. Feans), licensed under CC-BY-2.0
Make sure you have a working Go environment (Go 1.12 or higher is required). See the install instructions.
To install smartcrop, simply run:
go get github.com/muesli/smartcrop
To compile it from source:
git clone https://github.com/muesli/smartcrop.git
cd smartcrop
go build
package main
import (
"fmt"
"image"
_ "image/png"
"os"
"github.com/muesli/smartcrop"
"github.com/muesli/smartcrop/nfnt"
)
func main() {
f, _ := os.Open("image.png")
img, _, _ := image.Decode(f)
analyzer := smartcrop.NewAnalyzer(nfnt.NewDefaultResizer())
topCrop, _ := analyzer.FindBestCrop(img, 250, 250)
// The crop will have the requested aspect ratio, but you need to copy/scale it yourself
fmt.Printf("Top crop: %+v\n", topCrop)
type SubImager interface {
SubImage(r image.Rectangle) image.Image
}
croppedimg := img.(SubImager).SubImage(topCrop)
// ...
}
Also see the test cases in smartcrop_test.go and cli application in cmd/smartcrop/ for further working examples.
go install github.com/muesli/smartcrop/cmd/smartcrop
Usage of smartcrop:
-height int
crop height
-input string
input filename
-output string
output filename
-quality int
jpeg quality (default 85)
-resize
resize after cropping (default true)
-width int
crop width
Example: smartcrop -input examples/gopher.jpg -output gopher_cropped.jpg -width 300 -height 150
You can find a bunch of test images for the algorithm here.
Got some feedback or suggestions? Please open an issue or drop me a note!