{"id":13396863,"url":"https://github.com/jwagner/smartcrop.js","last_synced_at":"2025-05-10T01:28:09.101Z","repository":{"id":15668308,"uuid":"18405734","full_name":"jwagner/smartcrop.js","owner":"jwagner","description":"Content aware image cropping","archived":false,"fork":false,"pushed_at":"2024-03-16T23:45:27.000Z","size":23243,"stargazers_count":12887,"open_issues_count":23,"forks_count":575,"subscribers_count":213,"default_branch":"main","last_synced_at":"2025-05-05T14:01:36.375Z","etag":null,"topics":["image-processing","javascript","nodejs","smartcrop"],"latest_commit_sha":null,"homepage":"http://29a.ch/2014/04/03/smartcrop-content-aware-image-cropping","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jwagner.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-04-03T14:21:19.000Z","updated_at":"2025-05-04T15:10:31.000Z","dependencies_parsed_at":"2024-06-18T11:26:37.374Z","dependency_job_id":null,"html_url":"https://github.com/jwagner/smartcrop.js","commit_stats":{"total_commits":114,"total_committers":14,"mean_commits":8.142857142857142,"dds":"0.14912280701754388","last_synced_commit":"de2d1438b1f46490452f8e8b196974b19b0b0dcf"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwagner%2Fsmartcrop.js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwagner%2Fsmartcrop.js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwagner%2Fsmartcrop.js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwagner%2Fsmartcrop.js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwagner","download_url":"https://codeload.github.com/jwagner/smartcrop.js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252512855,"owners_count":21760085,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["image-processing","javascript","nodejs","smartcrop"],"created_at":"2024-07-30T18:01:05.253Z","updated_at":"2025-05-07T22:26:05.736Z","avatar_url":"https://github.com/jwagner.png","language":"JavaScript","funding_links":[],"categories":["JavaScript","Tools","目录","image-processing","Packages","13. 页面交互","nodejs","Graphics and Media","Uncategorized","📦 Legacy \u0026 Inactive Projects","JavaScript Libs"],"sub_categories":["13.5 图片剪裁/图片处理 ###","Image and Video","13.5 图片剪裁/图片处理/图片转换","Uncategorized","Misc"],"readme":"# smartcrop.js\n\n![example workflow](https://github.com/jwagner/smartcrop.js/actions/workflows/tests.yml/badge.svg)\n\nSmartcrop.js implements an algorithm to find good crops for images.\nIt can be used in the browser, in node or via a CLI.\n\n![Example](https://29a.ch/sandbox/2014/smartcrop/example.jpg)\nImage: [https://www.flickr.com/photos/endogamia/5682480447/](https://www.flickr.com/photos/endogamia/5682480447) by Leon F. Cabeiro (N. Feans), licensed under CC-BY-2.0\n\n\u003e **Note**\n\u003e I'm currently working on a more advanced version of smartcrop.js based on machine learning. As part of that I'm looking for a large dataset of manually cropped images. If you know of such a dataset, please [let me know](https://29a.ch/about).\n\n## Demos\n\n- [Smartcrop.js Test Suite](https://29a.ch/sandbox/2014/smartcrop/examples/testsuite.html), contains over 100 images, **heavy**.\n- [Smartcrop.js Test Bed](https://29a.ch/sandbox/2014/smartcrop/examples/testbed.html), allows you to test smartcrop with your own images and different face detection libraries.\n- [Automatic Photo transitions](https://29a.ch/sandbox/2014/smartcrop/examples/slideshow.html), automatically creates Ken Burns transitions for a slide show.\n\n## Simple Example\n\n```javascript\n// you pass in an image as well as the width \u0026 height of the crop you\n// want to optimize.\nsmartcrop.crop(image, { width: 100, height: 100 }).then(function(result) {\n  console.log(result);\n});\n```\n\nOutput:\n\n```javascript\n// smartcrop will output you its best guess for a crop\n// you can now use this data to crop the image.\n{topCrop: {x: 300, y: 200, height: 200, width: 200}}\n```\n\n## Download/ Installation\n\n`npm install smartcrop`\nor just download [smartcrop.js](https://raw.githubusercontent.com/jwagner/smartcrop.js/master/smartcrop.js) from the git repository.\n\nSmarcrop requires support for [Promises](http://caniuse.com/#feat=promises),\nuse a [polyfill](https://github.com/taylorhakes/promise-polyfill) for unsupported browsers or set `smartcrop.Promise` to your favorite promise implementation\n(I recommend [bluebird](http://bluebirdjs.com/)).\n\n\n## Consider avoiding crops using dont-crop\n\nIf you are interested in using smartcrop.js to crop your images you should also consider to avoid cropping them by using [dont-crop](https://github.com/jwagner/dont-crop/).\nDont-crop gives you matching gradients and colors to pad and complement your images.\n\n![Example](https://29a.ch/images/dont-crop.cache-399897619c3de2e0.jpg)\n\n## Command Line Interface\n\nThe [smartcrop-cli](https://github.com/jwagner/smartcrop-cli) offers command line interface to smartcrop.js.\n\n## Node\n\nYou can use smartcrop from nodejs via either [smartcrop-gm](https://github.com/jwagner/smartcrop-gm) (which is using image magick via gm) or [smartcrop-sharp](https://github.com/jwagner/smartcrop-sharp) (which is using libvips via sharp).\nThe [smartcrop-cli](https://github.com/jwagner/smartcrop-cli) can be used as an example of using smartcrop from node.\n\n## Stability\n\nWhile _smartcrop.js_ is a small personal project it is currently being used on high traffic production sites.\nIt has a basic set of automated tests and a test coverage of close to 100%.\nThe tests run in all modern browsers thanks to [saucelabs](https://saucelabs.com/).\nIf in any doubt the code is short enough to perform a quick review yourself.\n\n## Algorithm Overview\n\nSmartcrop.js works using fairly dumb image processing. In short:\n\n1. Find edges using laplace\n1. Find regions with a color like skin\n1. Find regions high in saturation\n1. Boost regions as specified by options (for example detected faces)\n1. Generate a set of candidate crops using a sliding window\n1. Rank them using an importance function to focus the detail in the center\n   and avoid it in the edges.\n1. Output the candidate crop with the highest rank\n\n## Face detection\n\nThe smartcrop algorithm itself is designed to be simple, relatively fast, small and generic.\n\nIn many cases it does make sense to add face detection to it to ensure faces get the priority they deserve.\n\nThere are multiple javascript libraries which can be easily integrated into smartcrop.js.\n\n- [ccv js](https://github.com/liuliu/ccv) / [jquery.facedetection](http://facedetection.jaysalvat.com/)\n- [tracking.js](https://trackingjs.com/examples/face_hello_world.html)\n- [opencv.js](https://docs.opencv.org/3.3.1/d5/d10/tutorial_js_root.html)\n- [node-opencv](https://github.com/peterbraden/node-opencv)\n\nYou can experiment with all of these in the [smartcrop.js testbed](https://29a.ch/sandbox/2014/smartcrop/examples/testbed.html)\n\nOn the client side I would recommend using tracking.js because it's small and simple. Opencv.js is compiled from c++ and very heavy (~7.6MB of javascript + 900kb of data).\njquery.facedetection has dependency on jquery and from my limited experience seems to perform worse than the others.\n\nOn the server side node-opencv can be quicker but comes with some [annoying issues](https://github.com/peterbraden/node-opencv/issues/415) as well.\n\nIt's also worth noting that all of these libraries are based on the now dated [viola-jones](https://en.wikipedia.org/wiki/Viola%E2%80%93Jones_object_detection_framework) object detection framework.\nIt would be interesting to see how more [state of the art](http://mmlab.ie.cuhk.edu.hk/projects/WIDERFace/WiderFace_Results.html) techniques could be implemented in browser friendly javascript.\n\n## Supported Module Formats\n\n- CommonJS\n- AMD\n- global export / window\n\n## Supported Browsers\n\nSee [caniuse.com/canvas](http://caniuse.com/canvas).\nA [polyfill](https://github.com/taylorhakes/promise-polyfill) for\n[Promises](http://caniuse.com/#feat=promises) is recommended if you need to support old browsers.\n\n## API\n\n### smartcrop.crop(image, options)\n\nFind the best crop for _image_ using _options_.\n\n**image:** anything ctx.drawImage() accepts, usually HTMLImageElement, HTMLCanvasElement or HTMLVideoElement.\n\nKeep in mind that [origin policies](https://en.wikipedia.org/wiki/Same-origin_policy) apply to the image source.\nYou may not use cross-domain images without [CORS](https://en.wikipedia.org/wiki/Cross-origin_resource_sharing) clearance.\n\n**options:** [cropOptions](#cropOptions)\n\n**returns:** A promise for a [cropResult](#cropResult).\n\n### cropOptions\n\n**minScale:** minimal scale of the crop rect, set to 1.0 to prevent smaller than necessary crops (lowers the risk of chopping things off).\n\n**width:** width of the crop you want to use.\n\n**height:** height of the crop you want to use.\n\n**boost:** optional array of regions whose 'interestingness' you want to boost (for example faces). See [boost](#boost);\n\n**ruleOfThirds:** optional boolean if set to false it will turn off the rule of thirds composition weight.\n\n**debug _(internal)_:** if true, cropResults will contain a debugCanvas and the complete results array.\n\nThere are many more (for now undocumented) options available.\nCheck the [source](smartcrop.js#L32) and be advised that they might change in the future.\n\n### cropResult\n\nResult of the promise returned by smartcrop.crop.\n\n```javascript\n{\n  topCrop: crop;\n}\n```\n\n### crop\n\nAn individual crop.\n\n```javascript\n{\n  x: 11, // pixels from the left side\n  y: 20, // pixels from the top\n  width: 1, // pixels\n  height: 1 // pixels\n}\n```\n\n### boost\n\nDescribes a region to boost. A usage example of this is to take\ninto account faces in the image. See [smartcrop-cli](https://github.com/jwagner/smartcrop-cli) for an example on how to integrate face detection.\n\n```javascript\n{\n  x: 11, // pixels from the left side\n  y: 20, // pixels from the top\n  width: 32, // pixels\n  height: 32, // pixels\n  weight: 1 // in the range [0, 1]\n}\n```\n\nNote that the impact the boost has is proportional to it's weight and area.\n\n## Tests\n\nYou can run the tests using `grunt test`. Alternatively you can also just run grunt (the default task) and open \u003chttp://localhost:8000/test/.\u003e\n\n## Benchmark\n\nThere are benchmarks for both the browser (test/benchmark.html) and node (node test/benchmark-node.js [requires node-canvas])\nboth powered by [benchmark.js](http://benchmarkjs.com).\n\nIf you just want some rough numbers: It takes **\u003c 20 ms** to find a **square crop** of a **640x427px** picture on an i7.\nIn other words, it's fine to run it on one image, it's suboptimal to run it on an entire gallery on page load.\n\n## Contributors\n\n- [Christian Muehlhaeuser](https://github.com/muesli)\n\n## Ports, Alternatives\n\n- [connect-thumbs](https://github.com/inadarei/connect-thumbs) Middleware for connect.js that supports smartcrop.js by [Irakli Nadareishvili](https://github.com/inadarei/connect-thumbs)\n- [smartcrop-java](https://github.com/QuadFlask/smartcrop-java) by [QuadFlask](https://github.com/QuadFlask/)\n- [smartcrop-android](https://github.com/QuadFlask/smartcrop-android) by [QuadFlask](https://github.com/QuadFlask/)\n- [smartcrop.go](https://github.com/muesli/smartcrop) by [Christian Muehlhaeuser](https://github.com/muesli)\n- [smartcrop.py](https://github.com/hhatto/smartcrop.py) by [Hideo Hattori](http://www.hexacosa.net/about/)\n- [smartcrop-rails](https://github.com/sadiqmmm/smartcrop-rails) smartcrop wrapped in a ruby gem by [Mohammed Sadiq](https://github.com/sadiqmmm/)\n- [smartcrop.net](https://github.com/softawaregmbh/smartcrop.net) c# .net port by [softaware gmbh](https://www.softaware.at/)\n- [dont-crop](https://github.com/jwagner/dont-crop/) a library to avoid cropping by padding images with matching colors or gradients\n\n## Version history\n\n### 2.0.5\nFix `TS1046: Top-level declarations in .d.ts files must start with either a 'declare' or 'export' modifier.`.\n\n### 2.0.4\nTypescript type definitions.\n\n### 2.0.2\n\nIn short: It's a lot faster when calculating bigger crops.\nThe quality of the crops should be comparable but the results\nare going to be different so this will be a major release.\n\n### 1.1.1\n\nRemoved useless files from npm package.\n\n### 1.1\n\nCreating github releases. Added options.input which is getting passed along to iop.open.\n\n### 1.0\n\nRefactoring/cleanup to make it easier to use with node.js (dropping the node-canvas dependency) and enable support for boosts which can be used to do face detection.\nThis is a 1.0 in the semantic meaning (denoting backwards incompatible API changes).\nIt does not denote a finished product.\n\n## License\n\nCopyright (c) 2018 Jonas Wagner, licensed under the MIT License (enclosed)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwagner%2Fsmartcrop.js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwagner%2Fsmartcrop.js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwagner%2Fsmartcrop.js/lists"}