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 🙏

© 2025 – Pkg Stats / Ryan Hefner

entropize

v1.0.1

Published

A versatile JavaScript library for analyzing image entropy and intelligently positioning/cropping images based on regions of high visual interest.

Readme

Image Entropy Analyzer

A versatile JavaScript library for analyzing image entropy and intelligently positioning/cropping images based on regions of high visual interest.

Features

  • 🔍 Analyzes image entropy to identify visually interesting regions
  • 🖼️ Calculates optimal image positioning and cropping parameters
  • 🔄 Automatic environment detection and appropriate resizing method selection
  • 📏 Respects minimum visible image area constraints
  • 🎯 Generates both CSS positioning and Sharp.js parameters
  • 🔗 Supports chained API operations
  • 🎨 Debug visualization mode for entropy regions

Installation

npm install entropize

Usage

Basic Usage

import Entropize from 'entropize';
const analyzer = new Entropize();

// With direct image URL
analyzer.analyze('path/to/image.jpg', {
  containerDimensions: { width: 800, height: 600 },
  minPercentage: 60
})
.then(async analyzer => {
  // Get analysis results
  const { cssImage, resizedImage } = analyzer.analysisResult;
  
  // Apply CSS positioning
  const container = document.querySelector('.image-container');
  Object.assign(container.style, cssImage);
  
  // Get resized image
  const resizedImg = await analyzer.resize();
  document.body.appendChild(resizedImg);
});

// With DOM element (automatically gets image and dimensions)
const imageContainer = document.querySelector('.image-container');
analyzer.analyze(imageContainer)
.then(async analyzer => {
  const { cssImage } = analyzer.analysisResult;
  Object.assign(imageContainer.style, cssImage);
});

Configuration Options

const analyzer = new ImageEntropyAnalyzer({
  blockSize: 16,        // Size of blocks for entropy analysis
  highEntropyThreshold: 0.2,  // Top 20% of regions considered high entropy
  debug: true          // Enable visual debug overlay
});

Analysis Options

analyzer.analyze(imageUrl, {
  containerDimensions: {
    width: 800,    // Target container width
    height: 600    // Target container height
  },
  minPercentage: 60    // Minimum percentage of image to show (default: 50)
});

Analysis Result Structure

{
  cssImage: {
    width: "1200px",
    height: "800px",
    position: "absolute",
    left: "-200px",
    top: "-100px",
    backgroundPosition: "60% 40%",
    objectFit: "cover"
  },
  resizedImage: {
    width: 800,
    height: 600,
    fit: "cover",
    position: {
      left: 100,
      top: 50,
      width: 600,
      height: 450
    }
  },
  entropyMap: [...],  // Array of high entropy regions
  entropyCenter: { x: 320, y: 240 },  // Weighted center of entropy
  originalSize: { width: 1000, height: 750 }
}

API Reference

Constructor

const analyzer = new ImageEntropyAnalyzer(options?)

Options:

  • blockSize: Size of blocks for entropy analysis (default: 16)
  • highEntropyThreshold: Proportion of high entropy regions to consider (default: 0.2)
  • debug: Enable debug visualization (default: false)

Methods

analyze(source, options?)

Analyzes the image and returns the analyzer instance for chaining.

Parameters:

  • source: Can be either:
    • Path or URL to the image
    • DOM element (img tag or element with background-image)
  • options:
    • containerDimensions: Target container size (optional, automatically inferred if DOM element provided)
    • minPercentage: Minimum visible image percentage (default: 50)

When passing a DOM element:

  • Automatically extracts image URL from src attribute or background-image
  • Infers container dimensions if not explicitly provided
  • Works with both tags and elements using background-image

Returns: Promise

resize()

Resizes the image based on analysis results.

Returns: Promise

Advanced Usage Examples

Custom Container Sizing

const analyzer = new ImageEntropyAnalyzer();

analyzer.analyze('image.jpg', {
  containerDimensions: { width: 1200, height: 800 },
  minPercentage: 75  // Show at least 75% of the image
})
.then(async analyzer => {
  const { cssImage, resizedImage } = analyzer.analysisResult;
  
  // Apply positioning
  const container = document.querySelector('.image-container');
  Object.assign(container.style, cssImage);
  
  // Container should be positioned relative
  container.style.position = 'relative';
  container.style.overflow = 'hidden';
});

Debug Visualization

const analyzer = new ImageEntropyAnalyzer({
  debug: true,
  blockSize: 32  // Larger blocks for visualization
});

analyzer.analyze('image.jpg')
  .then(analyzer => {
    // Debug overlay will be added to document
    // Yellow regions show high entropy areas
    // Red dot shows weighted center
  });

Processing Multiple Images

async function processImages(images) {
  const analyzer = new ImageEntropyAnalyzer();
  
  const results = await Promise.all(
    images.map(async img => {
      const instance = await analyzer.analyze(img, {
        containerDimensions: { width: 800, height: 600 }
      });
      return await instance.resize();
    })
  );
  
  return results;
}

Environment Support

Browser

  • Uses Canvas API for image processing
  • Returns Image instances from resize()
  • Supports debug visualization
  • Works with URLs and data URLs

Error Handling

try {
  const analyzer = new ImageEntropyAnalyzer();
  const instance = await analyzer.analyze('image.jpg');
  const result = await instance.resize();
} catch (error) {
  if (error.message.includes('Sharp operation failed')) {
    // Handle Sharp-specific errors
  } else {
    // Handle general errors
  }
}

Performance Considerations

  • Adjust blockSize based on image size and performance needs
  • Larger block sizes = faster analysis but less precise
  • Debug mode adds overhead - disable in production
  • Consider using Web Workers for browser processing of large images

License

MIT