Imagine.js — AI Image Generator Library for Node.js
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
orStability 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
.writeFileSync("city.png", image); fs
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();
.user(`You are an image remixing box.
llmGiven a text prompt, return a remixed prompt with more detail about the properties and qualities of a scene.
Only return a single prompt.`);
.assistant("Ok got it. What is your prompt?");
llm
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);
.writeFileSync("rose.png", buffer); fs
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(
, // Text input for image generation
input
{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 isa111
, a local provider. Other providers arestability
andreplicate
.model
<string>
: Image generation model. Default isa111
withStable 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");
.writeFileSync("rose.png", buffer); fs
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:
- AI.js — simple AI library
- Infinity Arcade — play any text adventure game
- Images Bot — image explorer
License
MIT
Author
Created by The Maximalist, see our open-source projects.