1.1.0 • Published 1 year ago

instantlight-js v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

InstantLight SDK for JavaScript

The InstantLight SDK allows you to manipulate lighting in images with or without prompts and change the background while editing the light. This SDK is easy to use and integrates with any JavaScript project.

Installation

Install the SDK using npm:

npm install instantlight-js

Usage/ Initialization

First, require the SDK in your JavaScript file and set up the API client with your API key and user email.

const { APIClient, ImageGeneration } = require('instantlight-js');

// Initialize API client
const apiClient = new APIClient('https://api.fotographer.ai/instantLight', 'your-api-key', 'your-email@example.com');

// Initialize ImageGeneration with the API client
const imageGeneration = new ImageGeneration(apiClient);

Modes of Operation

The SDK supports three modes of operation for editing images:

Mode 0: Edit light without a prompt and with a prompt Mode 2: Edit light while changing the background Mode 0: Edit Light Without a Prompt To edit the light in an image without using a prompt, set the mode parameter to 0.

const imageData = {
  foreground_image64: 'base64_encoded_foreground_image',
  background_image64: 'base64_encoded_background_image',
  prompt: '',
  mode: 0,
  prompt_strength: 0.0,
  inf_factor: 1.00,
  mask_strength: 0.5,
  image_width: 1400,
  image_height: 1400,
  additional_prompt: '',
  negative_prompt: '',
  lights: [] // Specify the lighting parameters as needed
};

imageGeneration.getImageGen(imageData)
  .then(response => {
    // Handle the response containing the edited image
    console.log(response);
  })
  .catch(error => {
    console.error('Error editing light:', error);
  });

Mode 0: Edit Light With a Prompt To edit the light in an image using a prompt, set the mode parameter to 1 and provide a prompt.

const imageData = {
  foreground_image64: 'base64_encoded_foreground_image',
  background_image64: 'base64_encoded_background_image',
  prompt: 'neon light',
  mode: 1,
  prompt_strength: 4.0,
  inf_factor: 1.00,
  mask_strength: 0.5,
  image_width: 1400,
  image_height: 1400,
  additional_prompt: '',
  negative_prompt: '',
  lights: [] // Specify the lighting parameters as needed
};

imageGeneration.getImageGen(imageData)
  .then(response => {
    // Handle the response containing the edited image
    console.log(response);
  })
  .catch(error => {
    console.error('Error editing light with prompt:', error);
  });

Mode 2: Edit Light and Change Background To edit the light and change the background of an image, set the mode parameter to 2.

const imageData = {
  foreground_image64: 'base64_encoded_foreground_image',
  background_image64: 'base64_encoded_background_image',
  prompt: 'sunset background',
  mode: 2,
  prompt_strength: 4.0,
  inf_factor: 1.00,
  mask_strength: 0.5,
  image_width: 1400,
  image_height: 1400,
  additional_prompt: '',
  negative_prompt: '',
  lights: [] // Specify the lighting parameters as needed
};

imageGeneration.getImageGen(imageData)
  .then(response => {
    // Handle the response containing the edited image
    console.log(response);
  })
  .catch(error => {
    console.error('Error editing light and changing background:', error);
  });

Handling API Responses Here's an example of how to Prepare the data and handle API responses, including saving the edited image and any mask image to disk:

const imageToBase64 = async (imagePath) => {
    const image = await loadImage(imagePath);
    const canvas = createCanvas(image.width, image.height);
    const ctx = canvas.getContext('2d');
    ctx.drawImage(image, 0, 0);
    return canvas.toDataURL().split(',')[1];
};

const imageDataMode0 = {
  foreground_image64: await imageToBase64('path_to_foreground_image.png'),
  background_image64: await imageToBase64('path_to_background_image.png'),
  prompt: '',
  mode: 0,
  prompt_strength: 0.0,
  inf_factor: 1.00,
  mask_strength: 0.5,
  image_width: 1400,
  image_height: 1400,
  additional_prompt: '',
  negative_prompt: '',
  lights: [] // Specify the lighting parameters as needed
};

imageGeneration.getImageGen(imageDataMode0)
  .then(response => {
    console.log('Response Keys:', Object.keys(response));

    if (response.image) {
      const imageBuffer = Buffer.from(response.image, 'base64');
      fs.writeFileSync('output_image_mode0.png', imageBuffer);
      console.log('Image retrieved and saved as output_image_mode0.png.');

      if (response.mask_image) {
        const maskBuffer = Buffer.from(response.mask_image, 'base64');
        fs.writeFileSync('output_mask_image_mode0.png', maskBuffer);
        console.log('Mask retrieved and saved as output_mask_image_mode0.png.');
      }
    } else {
      console.error("Response does not contain 'image'");
    }

    fs.writeFileSync('response_log_mode0.txt', JSON.stringify(response, null, 2));
  })
  .catch(error => {
    console.error('Error editing light:', error);
  });

API Reference APIClient The APIClient class handles the communication with the backend API.

Methods constructor(baseURL, apiKey, userEmail): Initializes the API client with the base URL, API key, and user email. post(endpoint, jsonData): Sends a POST request to the specified endpoint with the provided JSON data. ImageGeneration The ImageGeneration class uses the APIClient to edit images.

Methods constructor(apiClient): Initializes the ImageGeneration class with an APIClient instance. getImageGen(imageData): Edits the image based on the provided image data and returns a promise with the response.

1.1.0

1 year ago

1.0.0

1 year ago