smartcrop finds good image crops for arbitrary crop sizes

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

Owner
Christian Muehlhaeuser
Geek, Gopher, Software Developer, Maker, Opensource Advocate, Tech Enthusiast, Photographer, Board and Card Gamer
Christian Muehlhaeuser
Comments
  • 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.

  • 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.

  • 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?

  • 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.

  • 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!

  • 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, ...)

  • 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?

  • 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%
    
  • 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.

  • 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!

  • 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/

  • 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!

  • 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 😸

  • 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.

  • 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?

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

Jan 5, 2022
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

Feb 2, 2022
darkroom - An image proxy with changeable storage backends and image processing engines with focus on speed and resiliency.
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

Dec 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

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

Nov 25, 2021
Imgpreview - Tiny image previews for HTML while the original image is loading
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

May 22, 2022
Image processing algorithms in pure Go
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

Jan 6, 2023
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

Jan 2, 2023
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

Nov 11, 2022
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

Dec 17, 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

Jan 8, 2023
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

Jan 3, 2023
Imaging is a simple image processing package for Go
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

Dec 30, 2022
Pure golang image resizing
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 9, 2023
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

Sep 26, 2022
:triangular_ruler: Create beautiful generative image patterns from a string in golang.
: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

Dec 29, 2022
Go Image Filtering Toolkit
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

Dec 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

Jan 3, 2023
The imghdr module determines the type of image contained in a file for go
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

Oct 10, 2022