smartcrop

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2021 License: MIT Imports: 15 Imported by: 0

README

smartcrop

Build Status Coverage Status Go ReportCard GoDoc

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

Example 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

Example Image: https://www.flickr.com/photos/endogamia/5682480447 by Leon F. Cabeiro (N. Feans), licensed under CC-BY-2.0

Installation

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

To install smartcrop, simply run:

go get github.com/muesli/smartcrop

To compile it from source:

cd $GOPATH/src/github.com/muesli/smartcrop
go get -u -v
go build && go test -v

Example

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.

Simple CLI application

cd $GOPATH/src/github.com/muesli/smartcrop
go install ./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

Sample Data

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

Development

Join us on IRC: irc.freenode.net/#smartcrop

Documentation

Overview

Package smartcrop implements a content aware image cropping library based on Jonas Wagner's smartcrop.js https://github.com/jwagner/smartcrop.js

Package smartcrop implements a content aware image cropping library based on Jonas Wagner's smartcrop.js https://github.com/jwagner/smartcrop.js

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	DetailWeight:             0.2,
	SkinBias:                 0.01,
	SkinBrightnessMin:        0.2,
	SkinBrightnessMax:        1.0,
	SkinThreshold:            0.8,
	SkinWeight:               1.8,
	SaturationBrightnessMin:  0.05,
	SaturationBrightnessMax:  0.9,
	SaturationThreshold:      0.4,
	SaturationBias:           0.2,
	SaturationWeight:         0.3,
	ScoreDownSample:          8,
	Step:                     8,
	ScaleStep:                0.1,
	MinScale:                 0.9,
	MaxScale:                 1.0,
	EdgeRadius:               0.4,
	EdgeWeight:               -20.0,
	OutsideImportance:        -0.5,
	RuleOfThirds:             true,
	Prescale:                 true,
	PrescaleMin:              400.00,
	FaceDetectEnabled:        false,
	FaceDetectClassifierFile: "",
}
View Source
var (
	// ErrInvalidDimensions gets returned when the supplied dimensions are invalid
	ErrInvalidDimensions = errors.New("Expect either a height or width")
)
View Source
var FaceDetectConfig = Config{
	DetailWeight:             5.2,
	SkinBias:                 0.01,
	SkinBrightnessMin:        0.2,
	SkinBrightnessMax:        1.0,
	SkinThreshold:            0.8,
	SkinWeight:               5.8,
	SaturationBrightnessMin:  0.05,
	SaturationBrightnessMax:  0.9,
	SaturationThreshold:      0.4,
	SaturationBias:           0.2,
	SaturationWeight:         5.5,
	ScoreDownSample:          2,
	Step:                     8,
	ScaleStep:                0.1,
	MinScale:                 1.0,
	MaxScale:                 1.0,
	EdgeRadius:               0.4,
	EdgeWeight:               -20.0,
	OutsideImportance:        -0.5,
	RuleOfThirds:             true,
	Prescale:                 false,
	PrescaleMin:              400.0,
	FaceDetectEnabled:        true,
	FaceDetectClassifierFile: "",
}

FaceDetectConfig is a tweaked version of the DefaultConfig that has been optimised for smart cropping with face detection enabled.

Functions

This section is empty.

Types

type Analyzer

type Analyzer interface {
	FindBestCrop(img image.Image, width, height int) (image.Rectangle, error)
	FindAllCrops(img image.Image, width, height int) ([]Crop, error)
	FindFaces(img image.Image) []image.Rectangle
}

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(c Config, resizer options.Resizer) Analyzer

NewAnalyzer returns a new Analyzer using the given Resizer.

func NewAnalyzerWithLogger added in v0.3.0

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

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

func NewDebugAnalyzer added in v0.3.1

func NewDebugAnalyzer(c Config, resizer options.Resizer) Analyzer

NewDebugAnalyzer returns a new Analyzer using the given Resizer with debugging turned on.

type Config added in v0.3.1

type Config struct {
	DetailWeight float64

	SkinBias          float64
	SkinBrightnessMin float64
	SkinBrightnessMax float64
	SkinThreshold     float64
	SkinWeight        float64

	SaturationBrightnessMin float64
	SaturationBrightnessMax float64
	SaturationThreshold     float64
	SaturationBias          float64
	SaturationWeight        float64

	ScoreDownSample   int
	Step              int
	ScaleStep         float64
	MinScale          float64
	MaxScale          float64
	EdgeRadius        float64
	EdgeWeight        float64
	OutsideImportance float64
	RuleOfThirds      bool

	Prescale    bool
	PrescaleMin float64

	FaceDetectEnabled        bool
	FaceDetectClassifierFile string
}

type Crop

type Crop struct {
	image.Rectangle
	Score Score
}

Crop contains results

func (Crop) String added in v0.3.1

func (c Crop) String() string

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
	Face       float64
	Total      float64
}

Score contains values that classify matches

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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