MCPcopy Index your code
hub / github.com/muesli/smartcrop

github.com/muesli/smartcrop @v0.3.0 sqlite

repository ↗ · DeepWiki ↗ · release v0.3.0 ↗
43 symbols 110 edges 5 files 11 documented · 26% 3 cross-repo links
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 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.4 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 for further working examples.

Sample Data

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

Development

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

GoDoc Build Status Coverage Status Go ReportCard

Extension points exported contracts — how you extend this code

Analyzer (Interface)
Analyzer interface analyzes its struct and returns the best possible crop with the given width and height returns an err [1 …
smartcrop.go
Resizer (Interface)
Resizer is used to resize images. See the nfnt package for a default implementation using github.com/nfnt/resize. [1 implementers]
options/resizer.go
SubImager (Interface)
(no doc)
smartcrop_test.go

Core symbols most depended-on inside this repo

chop
called by 6
smartcrop.go
bounds
called by 5
smartcrop.go
debugOutput
called by 4
debug.go
writeImage
called by 4
debug.go
cie
called by 3
smartcrop.go
toRGBA
called by 3
smartcrop.go
SubImage
called by 2
smartcrop_test.go
totalScore
called by 2
smartcrop.go

Shape

Function 29
Method 6
Struct 5
Interface 3

Languages

Go100%

Modules by API surface

smartcrop.go25 symbols
smartcrop_test.go7 symbols
debug.go5 symbols
nfnt/resizer.go4 symbols
options/resizer.go2 symbols

For agents

$ claude mcp add smartcrop \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact