explaingit

jwagner/smartcrop.js

12,953JavaScriptAudience · developerComplexity · 2/5Setup · easy

TLDR

smartcrop.js automatically picks the most visually interesting crop region of an image by analyzing edges, skin tones, and color saturation, returns coordinates you apply with your own image code.

Mindmap

mindmap
  root((smartcrop.js))
    What it does
      Smart cropping
      Region scoring
      Coordinate output
    Tech stack
      JavaScript
      Node.js
      npm
    Analysis
      Edge detection
      Skin tone detection
      Color saturation
    Use cases
      Thumbnail generation
      Portrait cropping
      Batch processing
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Automatically crop user-uploaded images to thumbnail size without cutting off faces or key visual content.

USE CASE 2

Run server-side batch image cropping on a Node.js server using wrapper packages for common image tools.

USE CASE 3

Add face-aware portrait cropping by connecting a third-party face detection library to give detected faces extra weight.

USE CASE 4

Crop images from the command line using the companion smartcrop-cli tool.

Tech stack

JavaScriptNode.jsnpm

Getting it running

Difficulty · easy Time to first run · 5min

Face detection is not built in, integrate a third-party JavaScript face detection library manually if portrait accuracy matters.

In plain English

smartcrop.js is a JavaScript library for automatically choosing where to crop an image. Given an image and the dimensions you want the final crop to be, it analyzes the image and returns coordinates for the most visually interesting portion, rather than always cropping from the center or a fixed position. The algorithm uses basic image processing steps: it detects edges, looks for skin-tone colored regions, identifies highly saturated areas, and then slides a window across the image to score candidate crop rectangles. The crop with the highest score, weighted toward putting important content in the center and away from edges, is returned as the result. The output is a set of coordinates (x, y, width, height) that you then apply with your existing image handling code. The library runs in a web browser, in Node.js on a server, or from the command line via a companion tool called smartcrop-cli. In the browser it accepts standard image, canvas, or video elements. For server-side use, wrapper packages integrate it with common image processing tools. The README also points to a companion library called dont-crop, for situations where padding an image with matching background colors is preferable to cropping. Face detection is not built in, but the README explains how to connect several third-party JavaScript face detection libraries to give detected faces extra weight in the crop scoring. This helps ensure that portraits are cropped to keep faces in frame. The author notes in the README that a future machine-learning based version is in development. The current version is a personal project that has also been used on high-traffic production sites. It is available via npm and the source is short enough, as the README puts it, to read through quickly if you want to understand how it works.

Copy-paste prompts

Prompt 1
Using smartcrop.js in a browser, write JavaScript to analyze a user-uploaded image and crop it to 300x300 pixels keeping the most visually interesting region.
Prompt 2
I want to batch-process a folder of product images on a Node.js server with smartcrop.js, saving each cropped to a 16:9 ratio. Show me the code.
Prompt 3
How do I connect a JavaScript face detection library to smartcrop.js so that detected faces are weighted higher and stay in frame when cropping portraits?
Prompt 4
I have images where padding with a matching background color is better than cropping. How do I use the dont-crop companion library with smartcrop.js?
Open on GitHub → Explain another repo

← jwagner on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.