smartcrop

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2017 License: MIT Imports: 13 Imported by: 0

README

smartcrop

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 CC BY U.S. Fish & Wildlife

Example Image: https://www.flickr.com/photos/endogamia/5682480447 by N. Feans

Installation

Make sure you have a working Go environment. 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"
)

func main() {
        f, _ := os.Open("image.png")
        img, _, _ := image.Decode(f)

        analyzer := smartcrop.NewAnalyzer()
        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 crop_test.go for further working examples.

Sample Data

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

Development

API docs can be found here.

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

Build Status Coverage Status Go ReportCard

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 (
	// ErrInvalidDimensions gets returned when the supplied dimensions are invalid
	ErrInvalidDimensions = errors.New("Expect either a height or width")
)

Functions

func SmartCrop

func SmartCrop(img image.Image, width, height int) (image.Rectangle, error)

SmartCrop applies the smartcrop algorithms on the the given image and returns the top crop or an error if something went wrong. This is still here for legacy/backwards-compat reasons

Types

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() Analyzer

NewAnalyzer returns a new analyzer with default settings

func NewAnalyzerWithCropSettings

func NewAnalyzerWithCropSettings(cropSettings CropSettings) Analyzer

NewAnalyzerWithCropSettings returns a new analyzer with the given settings

type Crop

type Crop struct {
	image.Rectangle
	Score Score
}

Crop contains results

type CropSettings

type CropSettings struct {
	InterpolationType resize.InterpolationFunction
	DebugMode         bool
	Log               *log.Logger
}

CropSettings contains options to change cropping behaviour

type Score

type Score struct {
	Detail     float64
	Saturation float64
	Skin       float64
}

Score contains values that classify matches

Jump to

Keyboard shortcuts

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