smartcrop finds good image crops for arbitrary crop sizes

Overview

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

Issues
  • Decouple the resizer

    Decouple the resizer

    This commit adds a new Resizer interface with the resizer from nfnt as the default in its own package.

    This allows people to use the smartcrop library with their own resize implemention.

    My motivation for this is to use this in Hugo. We use github.com/disintegration/imaging to do image processing -- github.com/nfnt/resize is probably equally good, but imaging had a richer API for the Hugo use cases.

    I have tested this library and I'm really impressed by the results (and the speed):

    http://hugotest.bep.is/resourcemeta/smartcrop/

    The relevant Hugo PR is here:

    https://github.com/gohugoio/hugo/pull/4376

    I know this is breaking some API, but I think people are happy to adjust as long as it is for a greater good. And this is < 1.0 software, and it is super-easy to upgrade.

    Again, tanks for this great piece of software.

    enhancement 
    opened by bep 9
  • Fix build dependencies and library usage

    Fix build dependencies and library usage

    draw2d master is required since the latest stable v1 still uses googlecode.

    I added the haarscade file to the repository since the copyright of intel allows redistribution. Otherwise smartcrop would only work on linux.

    But it currently won't work outside of this repository, I'm still trying to find a generic solution.

    fixes #13 we can add more of the const options in other PRs.

    opened by sharpner 6
  • Cross compile error on MacOS for Linux (opencv)

    Cross compile error on MacOS for Linux (opencv)

    When I compile binary natively on MacOS - it's all ok:

    $ go build -o ./warehouse-macos github.com/me/warehouse/cmd/warehouse
    $ ls -lh ./warehouse-macos
    -rwxr-xr-x  1 user  user   9.2M Apr 24 16:22 ./warehouse-macos
    

    But when I try to compile on MacOS for Linux I get an error:

    $ GOOS=linux GOARCH=amd64 go build -o ./warehouse-linux github.com/me/warehouse/cmd/warehouse
    # github.com/muesli/smartcrop
    ../../../../muesli/smartcrop/crop.go:440: undefined: opencv.LoadHaarClassifierCascade
    ../../../../muesli/smartcrop/crop.go:443: undefined: opencv.FromImage
    

    What I doing wrong?

    invalid 
    opened by matyunin 4
  • Is this repository still alive?

    Is this repository still alive?

    There are many issues that are necessary for this repository to be useful again,

    • [ ] Remove fmt.Printf from source code
    • [ ] Update dep from code.google.com/p/draw2d/draw2d to its new repo
    • [ ] issue #14

    I would submit pull requests if you'd like, but if you've already abadoned this repository I'd rather fork it.

    What do you think? :/ would be a waste, since I think many people need this nowadays.

    opened by sharpner 4
  • Strategy for improving crops that miss

    Strategy for improving crops that miss

    Hey @muesli, for most images, this library is working perfectly. Every now and then, there are crops that need a little adjustment.

    Do you have any recommendations for improving the detection/crop model for images that crop incorrectly? I'd love to come up with a workflow for throwing a few before and after images into a folder, running ./train, etc...

    It might also be interesting to collect a library of models like the aarcascade_frontalface_alt.xml mentioned in the readme. E.g. one for animals, sides of faces, etc...

    Thanks for your time!

    question 
    opened by dancrew32 3
  • OpenCV 3.0

    OpenCV 3.0

    I'm getting the following error with OpenCV 3.0 installed. Just curious whether smartcrop is known to work with OpenCV 3.0. If not which version is safer to use?

    0.625 original resolution: 960x640 scale: 1.000000, cropw: 400.000000, croph: 400.000000, minscale: 1.000000 Time elapsed edge: 87.613185ms OpenCV Error: Unspecified error (The node does not represent a user object (unknown type?)) in cvRead, file /home/admin/source/opencv/opencv-3.0.0/modules/core/src/persistence.cpp, line 4976 terminate called after throwing an instance of 'cv::Exception' what(): /home/admin/source/opencv/opencv-3.0.0/modules/core/src/persistence.cpp:4976: error: (-2) The node does not represent a user object (unknown type?) in function cvRead

    SIGABRT: abort PC=0x7f7a6c6f7165 signal arrived during cgo execution

    goroutine 15 [syscall, locked to thread]: runtime.cgocall_errno(0x40c230, 0xc2081aae40, 0x0) /usr/local/go/src/runtime/cgocall.go:130 +0xf5 fp=0xc2081aae20 sp=0xc2081aadf8 github.com/lazywei/go-opencv/opencv._Cfunc_cvLoadHaarClassifierCascade(0x223d540, 0x100000001, 0x0) /home/admin/go/src/github.com/lazywei/go-opencv/opencv/:677 +0x44 fp=0xc2081aae40 sp=0xc2081aae20 github.com/lazywei/go-opencv/opencv.LoadHaarClassifierCascade(0xbcb430, 0x3e, 0x7f7a72ca8ac0) /home/admin/go/src/github.com/lazywei/go-opencv/opencv/cvaux.go:61 +0x90 fp=0xc2081aae78 sp=0xc2081aae40 github.com/muesli/smartcrop.faceDetect(0xc20819c3e0, 0xc2089ad180) /home/admin/go/src/github.com/muesli/smartcrop/crop.go:420 +0x173 fp=0xc2081ab088 sp=0xc2081aae78 github.com/muesli/smartcrop.analyse(0xc20819c3e0, 0x4079000000000000, 0x4079000000000000, 0x3ff0000000000000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...) /home/admin/go/src/github.com/muesli/smartcrop/crop.go:284 +0x31e fp=0xc2081ab2d8 sp=0xc2081ab088 github.com/muesli/smartcrop.SmartCrop(0xc20819d940, 0x280, 0x280, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)

    reported upstream 
    opened by sath33sh 3
  • Missing install instructions?

    Missing install instructions?

    I wanted to try your repository but I believe I am missing some system dependencies?

     go get github.com/muesli/smartcrop
    # pkg-config --cflags opencv opencv opencv opencv opencv opencv opencv
    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'opencv' found
    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'opencv' found
    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'opencv' found
    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'opencv' found
    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'opencv' found
    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'opencv' found
    Package opencv was not found in the pkg-config search path.
    Perhaps you should add the directory containing `opencv.pc'
    to the PKG_CONFIG_PATH environment variable
    No package 'opencv' found
    pkg-config: exit status 1
    

    Any hints?

    opened by sharpner 3
  • Precompute CIE values in edge detection for 2x speedup

    Precompute CIE values in edge detection for 2x speedup

    When running the test benchmark, I noticed that the dominant step was often edge detection. Upon investigation, it looked like there might be a lot of redundant calculations going on there. So I wrote a quick benchmark, and confirmed that precomputing the CIE values into a slice and reusing them saves a lot of time.

    Benchmark results:

    benchmark         old ns/op     new ns/op     delta
    BenchmarkEdge     192006874     78530474      -59.10%
    
    opened by swenson 3
  • The state of restoring face detection?

    The state of restoring face detection?

    Hello,

    I have been trying to use smartcrop for my use case (usually, making landscape thumbnails of portrait images of people), and in my completely unscientific investigations, it seems that the current smartcrop implementation picks any body part with no particular preference for face. That interpretation is supported by the fact that face detection has been dropped last year.

    Here are the smart crops of images on https://github.com/muesli/smartcrop/issues/33 - the issue is very similar to mine.

    38201198-f24a8300-36c9-11e8-8448-bda0f1b35686 38201199-f2952b1c-36c9-11e8-87e6-d898f944d060

    I understand that having native dependencies makes building more complicated and confusing, and therefore, it is good to have a pure Go package. OpenCV 3 seems to make things even more complicated.

    So, I remember seeing some comments about restoring OpenCV-based face detection as a separate optional package. What is the status of that effort? Would it be possible to somehow help with it?

    Also, I wonder if using https://github.com/hybridgroup/gocv would make integration with OpenCV 3 easier, since https://github.com/go-opencv/go-opencv seems to have some struggles with supporting OpenCV 3.

    opened by svkoskin 2
  • Update and clarify copyright of example images

    Update and clarify copyright of example images

    examples/goodtimes.jpg is now credited under a different (real?) name on Flickr;

    examples/gopher.jpg was relicensed from CC-BY-2.0 to CC-BY-NC-2.0 retrospectively circa November 2016.

    (We came across the discrepancies when we were filling out the copyright information for Debian packaging.)

    Many thanks!

    opened by anthonyfok 2
  • OpenCV problem

    OpenCV problem

    env: OS X 10.11.2 Go 1.5.2 OpenCV 2.4.12 Error when exec analyzer.FindBestCrop:

    stat /usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml: no such file or directory
    exit status 1
    

    path created by homebrew is /usr/local/Cellar/opencv/2.4.12/share/OpenCV, not /usr/share/opencv/

    invalid 
    opened by region23 2
  • Feature request: binary release for the CLI

    Feature request: binary release for the CLI

    I'd love to see a binary release for the cli tool - I'm after a single-binary smartcrop implementation to put on a docker image. There's the official smartcrop-cli, but that brings whole nodejs into the fold, bumping the size of the image substantially. Any chance of getting a binary build here, that I can then just wget in? :D

    Thanks!

    enhancement 
    opened by yacoob 1
  • Optional face detection with gocv

    Optional face detection with gocv

    I restored face detection features using another set of bindings for OpenCV, called gocv. It should still be possible to build smartcrop without having OpenCV installed. I have no particular interest in maintaining or supporting a fork, so if there is any chance for this to get merged, I'd appreciate it 😸

    enhancement 
    opened by svkoskin 7
  • Make opencv dependency optional

    Make opencv dependency optional

    Having a native Go smartcrop implementation can be more desired in some cases; looking at the code it shoud be quite trivial to move opencv dependency to a separate (sub)package, this way main smartcrop package can be made pure-Go and if one needs improved face detection, this can be done importing separate Analyzer from an opencv-dependent package.

    enhancement 
    opened by artyom 7
  • Using small sections for crops

    Using small sections for crops

    Hey there!

    I have a question and not sure how to solve this. I'm trying to detect a face and only cut out a rectangle with 200x200 size.

    Currently I am always getting crops like this: `smartcrop.Crop{X:0, Y:20, Width:598, Height:450, Score:smartcrop.Score{Detail:10.490105771557365, Saturation:55.443967954895136, Skin:271.62173185696514, Total:0.0029439244310097267}}``

    I searched the code a bit and found a minScale setting and a prescale setting that's always true.

    My first idea was to make them configurable and change prescaleMin to about 100. But that does not improve results.

    I am not to sure what's going on here. Can you give me a hint on how to solve this?

    opened by sharpner 1
Releases(v0.3.0)
Owner
Christian Muehlhaeuser
Geek, Gopher, Software Developer, Maker, Opensource Advocate, Tech Enthusiast, Photographer, Board and Card Gamer
Christian Muehlhaeuser
Resize upladed images to s3 bucket with given sizes, and uploades new images back to bucket

Features Resize upladed images to s3 bucket with given sizes, and uploades new images back to bucket Environment Variables IMAGE_SIZES - formax 200x20

null 1 Feb 2, 2022
Image - This repository holds supplementary Go image librariesThis repository holds supplementary Go image libraries

Go Images This repository holds supplementary Go image libraries. Download/Insta

null 0 Jan 5, 2022
darkroom - An image proxy with changeable storage backends and image processing engines with focus on speed and resiliency.

Darkroom - Yet Another Image Proxy Introduction Darkroom combines the storage backend and the image processor and acts as an Image Proxy on your image

Gojek 192 Jun 6, 2022
Easily customizable Social image (or Open graph image) generator

fancycard Easily customizable Social image (or Open graph image) generator Built with Go, Gin, GoQuery and Chromedp Build & Run Simply, Clone this rep

Youngbin Han 4 Jan 14, 2022
An API which allows you to upload an image and responds with the same image, stripped of EXIF data

strip-metadata This is an API which allows you to upload an image and responds with the same image, stripped of EXIF data. How to run You need to have

Cristina Simionescu 0 Nov 25, 2021
Imgpreview - Tiny image previews for HTML while the original image is loading

imgpreview This is a Go program that generates tiny blurry previews for images t

Dmitry Chestnykh 8 May 22, 2022
Image processing algorithms in pure Go

bild A collection of parallel image processing algorithms in pure Go. The aim of this project is simplicity in use and development over absolute high

Anthony N. Simon 3.6k Jun 26, 2022
Go package for fast high-level image processing powered by libvips C library

bimg Small Go package for fast high-level image processing using libvips via C bindings, providing a simple programmatic API. bimg was designed to be

Tom 1.9k Jun 22, 2022
Image processing library and rendering toolkit for Go.

blend Image processing library and rendering toolkit for Go. (WIP) Installation: This library is compatible with Go1. go get github.com/phrozen/blend

Guillermo Estrada 59 Dec 24, 2021
Decode embedded EXIF meta data from image files.

goexif Provides decoding of basic exif and tiff encoded data. Still in alpha - no guarantees. Suggestions and pull requests are welcome. Functionality

Robert Carlsen 522 Jun 24, 2022
A lightning fast image processing and resizing library for Go

govips A lightning fast image processing and resizing library for Go This package wraps the core functionality of libvips image processing library by

David Byttow 697 Jun 28, 2022
Fast, simple, scalable, Docker-ready HTTP microservice for high-level image processing

imaginary Fast HTTP microservice written in Go for high-level image processing backed by bimg and libvips. imaginary can be used as private or public

Tom 4.4k Jun 23, 2022
Imaging is a simple image processing package for Go

Imaging Package imaging provides basic image processing functions (resize, rotate, crop, brightness/contrast adjustments, etc.). All the image process

Grigory Dryapak 4.3k Jun 24, 2022
Pure golang image resizing

This package is no longer being updated! Please look for alternatives if that bothers you. Resize Image resizing for the Go programming language with

Jan Schlicht 2.8k Jun 26, 2022
Go package for decoding and encoding TARGA image format

tga tga is a Go package for decoding and encoding TARGA image format. It supports RLE and raw TARGA images with 8/15/16/24/32 bits per pixel, monochro

Sigrid Solveig HaflĂ­nudĂłttir 30 Mar 5, 2022
:triangular_ruler: Create beautiful generative image patterns from a string in golang.

geopattern Create beautiful generative image patterns from a string in golang. Go port of Jason Long's awesome GeoPattern library. Read geopattern's d

Pravendra Singh 1.2k Jun 9, 2022
Go Image Filtering Toolkit

GO IMAGE FILTERING TOOLKIT (GIFT) Package gift provides a set of useful image processing filters. Pure Go. No external dependencies outside of the Go

Grigory Dryapak 1.6k Jun 23, 2022
Go Perceptual image hashing package

goimagehash Inspired by imagehash A image hashing library written in Go. ImageHash supports: Average hashing Difference hashing Perception hashing Wav

Dong-hee Na 508 Jun 23, 2022
The imghdr module determines the type of image contained in a file for go

goimghdr Inspired by Python's imghdr Installation go get github.com/corona10/goimghdr List of return value Value Image format "rgb" SGI ImgLib Files

Dong-hee Na 38 Jan 23, 2022