npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

magic-crop

v1.0.4

Published

Library to automatically extract a photo from a screenshot

Downloads

6

Readme

MagicCrop

A JavaScript library to automatically detect and extract a photo from a screenshot.

Why use this? Many apps don't allow you to save images to your camera roll. You can work around this by taking a screenshot of the photo and manually cropping it using a third party app like Photoshop Express.

This library attempts to automate that process. Give it a screenshot containing a photo, and it will detect the cropping bounds of the photo and output a new HTML Canvas element containing the cropped image.

The algorithm is application agnostic and has been tested with screenshots taken from many different apps.

Extraction

Installation

bower install magic-crop
<script src="bower_components/magic-crop/src/magicCrop.js></script>

or

npm install magic-crop
var MagicCrop = require('magic-crop')

Usage

function crop(imageElem) {
    var magicCrop = new MagicCrop();
        
    // get the image data
    var imageData = magicCrop.getImageData(imageElem);
    
    // calculate the cropping bounds
    var bound = magicCrop.calcCroppingBounds(imageData.data.buffer, imageData.width, imageData.height);

    // perform the crop
    var croppedCanvas = magicCrop.cropToCanvas(imageElem, bound);

    // do something with the cropped canvas
    var croppedImage = new Image();
    croppedImage.src = croppedCanvas.toDataURL('image/png');
}
    
// load the URL to crop into an HTML Image element
var imageElem = new Image();
imageElem.src = 'screenshotToCrop.png';
imageElem.onload = function () {
    crop(imageElem);
}

Demo

Clone, install dev dependencies, then run the demo:

git clone https://github.com/mikechamberlain/magic-crop.git
cd magic-crop
npm install
npm run demo

The index.html page will open in your browser. Development

Clone, install dev dependencies, then run the tests:

git clone https://github.com/mikechamberlain/magic-crop.git
cd magic-crop
npm install
npm run test

The tests will run and watch for changes.

How it works

High level overview of the algorithm:

  1. Calculate most popular colors across the entire image. These are our background colors.
  2. Sample a bunch of points across the image and work towards each edge, looking for a background color found in 1.
  3. When we find a background color pixel, or we hit the edge, store this value as a potential bound.
  4. From the potential bounds calculated in 3, choose the most popular to represent our final crop region.
  5. Apply this crop region to our original image.

Crop Magic iPhone App

The MagicCrop library powers the Crop Magic iPhone application.

Available on the App Store