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

imaginesdk

v1.1.0

Published

Imagine SDK is a Node.js library that provides a convenient interface to interact with the Imagine API for image generation and manipulation. This README provides an overview of the library's features, installation instructions, and usage examples.

Downloads

5,351

Readme

Imagine SDK

Imagine SDK is a Node.js library that provides a convenient interface to interact with the Imagine API for image generation and manipulation. This README provides an overview of the library's features, installation instructions, and usage examples.

Table of Contents

Installation

To install the package, execute the following command:

npm install imaginesdk

Usage

The SDK needs to be configured with an API key which is available here. It will be passed to the Imagine client as an argument while instantiating it.

import { client, GenerationStyle, Status } from "imaginesdk";

// Initialize the client with your API key
const imagine = client("<YOUR_API_KEY>");

const main = async () => {
  // Generate an image with the Imagine API
  const response = await imagine.generations(
    `A vibrant and whimsical fantasy forest with magical creatures, glowing plants, and a flowing river, in a digital painting style inspired by video games like Ori and the Blind Forest.`,
    {
      style: GenerationStyle.IMAGINE_V5,
    }
  );

  // Check if the request was successful
  if (response.status() === Status.OK) {
    const image = response.getOrThrow();
    image.asFile("output.png");
  } else {
    console.log(response.errorOrThrow());
  }
};

//  Run the main function
main();

Result:

Generations

Imagine Client

The Imagine class acts as a facade, providing an interface to interact with all of our endpoints. It currently provides the following features:

  • Text-To-Image: generations() -> Response[Image]
  • Image-Remix: remix() -> Response[Image]
  • Upscale: upscale() -> Response[Image]
  • Variations: variations() -> Response[Image]
  • In-Painting: inpaint() -> Response[Image]

For the full list of parameters and other details, check out the documentation.

Response

Response is the return type for each of our functions. It contains the following:

  • status(): Status property which returns an enum containing the status code of the response.
  • data(): Either returns the response content or null in case of error.
  • getOrThrow(): Either returns the response content or raises an Error if the response content is empty.
  • getOrElse(): Either returns the response content or returns the default value if it's empty.
  • errorOrThrow(): Either returns the error details or raises an Error if the response status is successful.

For the full list of arguments and other details, check out the documentation.

Image

All the functions related to Images contain an Image data type as the data in their Response. It currently provides the following:

ArrayBuffer

Returns the ArrayBuffer received after a request operation

image.buffer(); // -> ArrayBuffer

asFile(filePath: string)

Stores the image in the File data type and return the file. It also stores the image in local directory

image.asFile("filePath"); // -> File (filePath)

asImageSrc

Returns the image which can be used as source to HTML tag.

image.asImageSrc(); // -> string

base64

Returns the base64 string after request operation

image.base64(); // -> string

blob

Returns the blob after request operation

image.blob(); // -> Blob

Some More Usage Examples

Variations

import { client, Status, VariationStyle } from "imaginesdk";

// Create a client with your API key
const imagine = client("<YOUR_API_KEY>");

const main = async () => {
  // Call the variations method with the text and image
  const response = await imagine.variations(
    `a cute anime girl in a forest`, // Prompt
    "anime-girl.png", // Can pass absolute or relative path, image url, or blob of image
    {
      style: VariationStyle.ANIME,
    }
  );

  // Check if the response is OK
  if (response.status() === Status.OK) {
    const image = response.getOrThrow();
    image.asFile("output.png");
  } else {
    console.log(response.errorOrThrow());
  }
};

//  Run the main function
main();

Result:

Variate

In-Painting

import { client, Status } from "imaginesdk";

// Create a client with your API key
const imagine = client("<YOUR_API_KEY>");

const main = async () => {
  // Call the method of the inpaint with the text and images
  const response = await imagine.inpaint(
    `woman sitting next to a teddy bear`, // Prompt text
    "couple.png", // Can pass absolute or relative path, image url, or blob of image
    "mask.png" // Can pass absolute or relative path, image url, or blob of mask
  );

  // Check if the response is OK
  if (response.status() === Status.OK) {
    const image = response.getOrThrow();
    image.asFile("output.png");
  } else {
    console.log(response.errorOrThrow());
  }
};

//  Run the main function
main();

Result: InPainting

Support

If you run into any version issues, please contact us at [email protected] or support.imagine.api

License

This project is licensed under the Apache-2.0 license.