package module
Version: v0.3.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Oct 30, 2018 License: MIT Imports: 13 Imported by: 67



smartcrop finds good image crops for arbitrary sizes. It is a pure Go implementation, based on Jonas Wagner's smartcrop.js

Example Image: by Washington Dept of Fish and Wildlife, originally licensed under CC-BY-2.0 when the image was imported back in September 2014

Example Image: by Leon F. Cabeiro (N. Feans), licensed under CC-BY-2.0


Make sure you have a working Go environment (Go 1.4 or higher is required). See the install instructions.

To install smartcrop, simply run:

go get

To compile it from source:

cd $GOPATH/src/
go get -u -v
go build && go test -v


package main

import (
	_ "image/png"


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 for further working examples.

Sample Data

You can find a bunch of test images for the algorithm here.


Join us on IRC:

GoDoc Build Status Coverage Status Go ReportCard



Package smartcrop implements a content aware image cropping library based on Jonas Wagner's smartcrop.js

Package smartcrop implements a content aware image cropping library based on Jonas Wagner's smartcrop.js



This section is empty.


View Source
var (
	// ErrInvalidDimensions gets returned when the supplied dimensions are invalid
	ErrInvalidDimensions = errors.New("Expect either a height or width")


This section is empty.


type Analyzer

type Analyzer interface {
	FindBestCrop(img image.Image, width, height int) (image.Rectangle, error)

Analyzer interface analyzes its struct and returns the best possible crop with the given width and height returns an error if invalid

func NewAnalyzer

func NewAnalyzer(resizer options.Resizer) Analyzer

NewAnalyzer returns a new Analyzer using the given Resizer.

func NewAnalyzerWithLogger added in v0.3.0

func NewAnalyzerWithLogger(resizer options.Resizer, logger Logger) Analyzer

NewAnalyzerWithLogger returns a new analyzer with the given Resizer and Logger.

type Crop

type Crop struct {
	Score Score

Crop contains results

type Logger added in v0.3.0

type Logger struct {
	DebugMode bool
	Log       *log.Logger

Logger contains a logger.

type Score

type Score struct {
	Detail     float64
	Saturation float64
	Skin       float64

Score contains values that classify matches


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL