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

@themaximalist/imagine.js

v0.0.5

Published

Simple AI Image Generation

Downloads

20

Readme

Imagine.js

Imagine.js is a simple AI image generator library for Node.js. It works with local models like Automatic1111 and remote models like Replicate and Stability.

await Imagine("a red rose"); // Buffer(...)

Features

  • Easy to use
  • Same interface for all services (a1111, replicate, stability)
  • Works with local Stable Diffusion models
  • Works with any remote models on Replicate or Stability AI
  • Create image prompts with LLMs for excellent results
  • MIT license

Install

Install Imagine.js from NPM:

npm install @themaximalist/imagine.js

For local models, ensure an Automatic1111 instance is running.

For remote models, make sure you have REPLICATE_API_KEY or STABILITY_API_KEY set in your environment variables.

export STABILITY_API_KEY=...
export REPLICATE_API_KEY=...

Usage

Imagine.js takes a text prompt and returns an image buffer.

const image = await Imagine("futuristic sci-fi city"); // image buffer
fs.writeFileSync("city.png", image);

The buffer can be saved to disk, written to a database, etc...

Image Generators

Specify a different image generator service, a1111, replicate or stability.

await Imagine("a red rose"); // defaults to a1111
await Imagine("a red rose", { service: "replicate"} );
await Imagine("a red rose", { service: "stability"} );

Making it easy to switch providers ensures you can try lots of combinations and prevent getting locked in!

LLM Prompt

Running your prompt through an LLM first can produce great results. Imagine.js is easy to combine with LLM.js to quickly remix and increase the quality of your prompts.

const LLM = require("@themaximalist/llm.js");

const llm = new LLM();

llm.user(`You are an image remixing box.
Given a text prompt, return a remixed prompt with more detail about the properties and qualities of a scene.
Only return a single prompt.`);
llm.assistant("Ok got it. What is your prompt?");

const prompt = await llm.chat("a red rose");
// a dew-kissed red rose in the early morning light, its petals ...

const buffer = await Imagine(prompt);
fs.writeFileSync("rose.png", buffer);

You can run this over and over and iterate with the LLM and Image Model towards a better and better result. Check out AI.js and Images Bot for a real-world examples of this.

API

The Imagine.js API is a simple function you call with your text prompt, and an optional config object.

await Imagine(
    input, // Text input for image generation
    {
        service: "stability", // Embedding service
        model: "stable-diffusion-xl-beta-v2-2-2", // Embedding model
        seed: 100, // Stabilize image generation
    }
);

Options

  • service <string>: Image generation service provider. Default is a111, a local provider. Other providers are stability and replicate.
  • model <string>: Image generation model. Default is a111 with Stable Diffusion
  • seed <int>: Stabilize image generation making it more deterministic. No default.

Response

Imagine.js returns an image Buffer. Typically you save this as a png file, which let's you view it.

const buffer = await Imagine("a red rose");
fs.writeFileSync("rose.png", buffer);

The Imagine.js API ensures you have a simple way to use different image generators in the same interface.

Debug

Imagine.js uses the debug npm module with the imagine.js namespace.

View debug logs by setting the DEBUG environment variable.

> DEBUG=imagine.js*
> node src/imagine_images.js
# debug logs

Projects

Imagine.js is currently used in the following projects:

License

MIT

Author

Created by The Maximalist, see our open-source projects.