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

photosaic

v0.2.1

Published

Build beautiful mosaic images from input images and photos you provide.

Downloads

7

Readme

photosaic

Create beautiful mosaics of an image from an original you specify, and are made up of many small images/photos you also provide!

Install

$ npm install -s photosaic

Usage

import fs from 'fs'
import Photosaic from 'photosaic'

const mosaic = Photosaic(`./targetImgForMosaic.png`, [
  `./subImg1.png`,
  `./subImg2.png`,
  `./subImg3.png`,
])
const finalMosaicBuffer = await mosaic.build()
await fs.promises.writeFile(`./finalMosaic.png`, finalMosaicBuffer)

Main Image Type

  • type PhotosaicImage = string | Buffer | Readable: main abstraction of an "image" used by Photosaic
    • string: the full file path of the image on the local file system
    • Buffer: a raw buffer of an image
    • Readable: a readable stream of an image to be piped to a writable stream

Constructor

  • Photosaic(sourceImg, subImages, options?): factory function containing functionality to build mosaics
    • sourceImg: PhotosaicImage: The image of the final mosaic that will be created
    • subImages: PhotosaicImage[]: The small, subImages that will be used to build the mosaic
    • options?: additional options you can provide to customize the output mosaic created
      • options.gridNum?: number = 10: The final mosaic will be made up of a gridNum x gridNum grid of subImages
      • options.intensity?: number = 0.5: Number between 0-1 indicating the opacity of the shading on the subImages to help make the output image clearer. 0 is fully transparent shading (main image will be impossible to see), 1 is fully opaque (subImages will be impossible to make out). The default of 0.5 should be fine in most cases.
      • options.outputType?: string = 'png': The output type of the final mosaic. To preserve transparency in the image, use the default, 'png'.
      • options.outputWidth?: number = 400: Number of pixels the output mosaic's width will be (height will auto scale). The larger the width, the bigger the mosaic and the larger in size the final mosaic will be. The larger the output the longer it takes to generate a mosaic
      • options.algo?: 'closestColor' | 'random' = 'closestColor': How the subImages will be dispersed throughout when building the mosaic.
        • 'random' selects one of the subImages randomly each iteration to be inserted in that slice of the mosaic
        • 'closestColor' selects the subImage that is closest to the average color of the slice of the main image that the subImage is getting inserted to build the mosaic.

options.algo differences

closestColor

random

Methods

Assuming const photosaic = Photosaic(source, subImgs, opts), the methods below are exposed on photosaic

  • photosaic.build(): Promise<Buffer>: create a mosaic and return the result in a raw Buffer
  • photosaic.setSourceImage(newSrc: PhotosaicImage) => PhotosaicImage: reset source image created in mosaic
  • photosaic.setSubImages(subImgs: PhotosaicImage[]) => PhotosaicImage[]: reset subImages used to build mosaic
  • photosaic.addSubImage(img: PhotosaicImage): PhotosaicImage[]: add an image to the subImage list to build mosaic
  • photosaic.imgToStream(img: PhotosaicImage): Readable: convert a PhotosaicImage to a Readable stream
  • photosaic.imgToBuffer(img: PhotosaicImage): Promise<Buffer>: convert a PhotosaicImage to a raw Buffer

Tracking Mosaic Progress

Depending on the options provided (specifically gridNum and outputWidth) and the hardware you're running to build the mosaic, it could take several to tens of minutes for photosaic.build() to complete. Therefore, photosaic has an EventEmitter, photosaic.emitter you can listen for processing events to get the progress of the mosaic being built.

There will be a total of 2 * gridNum^2 iterations processed.

const photosaic = Photosaic(source, subImgs, opts)
photosaic.emitter.on('processing', (iteration) => {
  console.log(`Currently processing '${iteration}' subImage for the mosaic`)
})
await photosaic.build()

// Currently processing '1' subImage for the mosaic
// Currently processing '2' subImage for the mosaic
// Currently processing '3' subImage for the mosaic
// ...

CLI Task

You can use the following task to create a mosaic via the CLI with files on your local file system.

$ npm install -s photosaic
$ node ./node_modules/photosaic/dist/tasks/createMosaic.js -i ./pathToMosaicFile.js --dir ./path/to/sub/images -g 40 -w 1000