Fork me on GitHub

Imagine.js — AI Image Generator Library for Node.js

Imagine.js

GitHub Repo stars NPM Downloads GitHub code size in bytes GitHub License


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(...)
Example of Imagine.js image generation Example of Imagine.js image generation Example of Imagine.js image generation Example of Imagine.js image generation

Features

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

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.