GithubHelp home page GithubHelp logo

imclab / smartcrop.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jwagner/smartcrop.js

0.0 2.0 0.0 18.07 MB

Content aware image cropping

License: MIT License

JavaScript 98.64% CSS 1.36%

smartcrop.js's Introduction

smartcrop.js

โš ๏ธ This is a work in progress. Neither the API nor the algorithm are finished or battle proofen.

Smartcrop.js implements an algorithm to find good crops for images.

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

It does this using fairly dumb image processing. In short:

  1. Find edges using laplace
  2. Find regions with a color like skin
  3. Find regions high in saturation
  4. Generate a set of candidate crops
  5. Rank them using a importance function to focus the detail in the center and avoid it in the edges.
  6. Output the candidate crop with the highest rank

Demos

Simple Example

SmartCrop.crop(image, {width: 100, height: 100}, function(result){console.log(result);});
// {topCrop: {x: 300, y: 200, height: 200, width: 200}}

Download/ Installation

npm install smartcrop or bower install smartcrop or just download smartcrop.js from the git repo.

CLI / Node.js

The smartcrop-cli offers command line interface to smartcrop.js. It is based on node.js and node-canvas. You can also view it as an example on how to use smartcrop.js from a node.js app.

Module Formats

Supported:

  • common js
  • amd
  • global export / window

Supported Browsers

See caniuse.com/canvas

API

The API is not yet finalized. Look at the code for details and expect changes.

SmartCrop.crop(image, options, callback)

Crop image using options and call callback(result) when done.

image: anything ctx.drawImage() accepts, usually HTMLImageElement, HTMLCanvasElement or HTMLVideoElement

options: see cropOptions

callback: function(cropResult)

cropOptions

debug: if true, cropResults will contain a debugCanvas

minScale: minimal scale of the crop rect, set to 1.0 to prevent smaller than necessary crops (lowers the risk of chopping things off).

width: width of the crop you want to use.

height: height of the crop you want to use.

There are many more (for now undocumented) options available. Check the source and know that they might change in the future.

cropResult

{
  topCrop: crop,
  crops: [crop]
}

crop

{
  x: 1,
  y: 1,
  width: 1,
  height: 1
}

Tests

You can run the tests using grunt test. Alternatively you can also just run grunt (the default task) and open http://localhost:8000/test/. The test coverage for smartcrop.js is very limited at the moment. I expect to improve this as the code matures and the concepts solidify.

Benchmark

There are benchmarks for both the browser (test/benchmark.html) and node (node test/benchmark-node.js [requires node-canvas]) both powered by benchmark.js.

If you just want some rough numbers: It takes < 100 ms to find a square crop of a 640x427px picture on an i7. In other words, it's fine to run it on one image, it's not cool to run it on an entire gallery on page load.

Contributors

Ports, Alternatives

License

Copyright (c) 2014 Jonas Wanger, licensed under the MIT License (enclosed)

smartcrop.js's People

Contributors

jwagner avatar muesli avatar pjackowski avatar

Watchers

JT5D avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.