Sample code for 30+ languages & platforms
React Native Requires Chilkat v11.4.0+

AI Modify Existing Image with Text Prompt

See more AI Examples

Uses an AI text prompt and uploaded image data to modify an existing image and receive the modified output.

Chilkat React Native Downloads

React Native
import { Ai, BinData, JsonObject } from '@chilkat/react-native'

async function chilkatExample() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  // Load the data from an existing image.
  const bdImageData = new BinData();
  try {
    bdImageData.loadFile('qa_data/jpg/kid_blue_coat.jpg');
  } catch {
    console.log(bdImageData.lastErrorText);
    return;
  }

  const ai = new Ai();

  ai.provider = 'openai';

  // Use your provider's API key.
  ai.apiKey = 'MY_API_KEY';

  // Choose a model.
  ai.model = 'gpt-5.6-terra';

  const askParams = new JsonObject();
  askParams.updateString('image.output_format', 'jpeg');
  ai.setAskParams(askParams);

  ai.inputAddImageData(bdImageData, '');
  ai.inputAddText('Modify the image by replacing the blue coat with a Metallica T-shirt.');

  // Give the AI some time (2 minutes).
  ai.idleTimeoutMs = 120000;

  // Ask the AI for image output.
  try {
    await ai.askAsync('image');
  } catch {
    console.log(ai.lastErrorText);
    return;
  }

  // Get the image response data.;
  try {
    ai.getOutputBd(bdImageData);
  } catch {
    console.log(ai.lastErrorText);
    return;
  }

  bdImageData.writeFile('c:/aaworkarea/out.jpg');

  console.log('Success.');

  // -------------------------------
  // Sample Input:
  // -------------------------------
  // image

  // -------------------------------
  // Sample Output:
  // -------------------------------
  // image
}