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

image-outline

v0.1.0

Published

Get a polygon outline for the opaque (or non-white or non-black) area of an image, in the browser, in node, or on the command line

Downloads

106

Readme

image-outline

Let's say you have a .png of a cow, with a transparent background. How many times have you asked yourself how you can get a polygon outline of the cow from that? Well, maybe it will come up someday if you are working on:

  1. Some sort of cow-outlining graphics demo
  2. Some kind of 2-D physics simulation of static-body cows (hey, that's how I came to this).
  3. I'm sure there are many other applications.

Well, now you are in luck. This module -- usable as a node module, in the browser, or on the command line -- can quickly and easily generate a polygon from any image of a cow.

It also works on images of things other than cows.

In node.js

When running in the node environment, image-outline uses get-pixels to asynchronously load the image, from a file or an URL.

Run

npm install --save image-outline

Then in your javascript

var getImageOutline = require('image-outline');

// Point getImageOutline at a relative file path or an URL to an image
// preferably one showing a PNG, with a transparent background, maybe of a cow. 

getImageOutline('http://www.cow.pics/cow.png', function(err, polygon) {
   if (err) {
      // err is an Error; handle it
      return;
   }
   
   // polygon is now an array of {x,y} objects. Have fun!
};

From the Browser

The browser version of the getImageOutline function takes a DOM Image Element (an HTMLImageElement), which must already be done loading its content, and synchronously returns a polygon.

Behind-the-scenes, an HTML5 canvas is used to extract pixel data from the image, so this isn't going to work in Internet Explorer 8 or earlier.

With browserify

Run

npm install --save image-outline

Then in your javascript

var getImageOutline = require('image-outline');

// Get your hands on a loaded HTMLImageElement, preferably one showing a PNG,
// with a transparent background, maybe of a cow. 
var imageElement = new Image();
image.href = 'http://www.cow.pics/cow.png';
image.onload = function() {
   var polygon = getImageOutline(image);
   // polygon is now an array of {x,y} objects. Have fun!
};

Then browserify your whole shebang and... well, you don't need me to tell you how browserify works.

Without browserify

Not using browserify yet? It's fun: it lets you have all the luxuries of npm in for-the-browser code. No? OK, no problem, the stand-alone prebuilt UMD version of image-outline will work in an AMD (requireJS) or vanilla JavaScript environment.

I will show only the vanilla JavaScript example here and trust that folks using requireJS know how to require a module.

Grab a copy of image-outline-min.js and put it somewhere your stuff can access.

In your html:

<script src="path/to/image-outline-min.js"></script>

(Manually maintaining script links in html is one reason you're always feeling so much pressure to use RequireJS or Browserify or something.)

This has added the getImageOutline function to the global window object. (Which is a little gross, another reason you're always feeling so much pressure to use RequireJS or Browserify or something.)

So now your JavaScript can work like this:

// Get your hands on a loaded HTMLImageElement, preferably one showing a PNG,
// with a transparent background, maybe of a cow. 
var imageElement = new Image();
image.href = 'http://www.cow.pics/cow.png';
image.onload = function() {
   var polygon = getImageOutline(image);
   // polygon is now an array of {x,y} objects. Have fun!
};

CLI

A command-line utility is available when you're working in the npm environment.

Run

npm install -g image-outline

To make imageoutline available as a globally-installed CLI module. (Pro tip: don't. Install it locally with --save-dev instead, and reference it from your package.json's "scripts" field instead.)

Then you can run

imageoutline http://www.cow.pics/cow.png

And get back a series of newline-separated x,y pairs on stdout.