Sample code for 30+ languages & platforms
Dart 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 Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // 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.
  final bdImageData = CkBinData();
  try {
    bdImageData.loadFile('qa_data/jpg/kid_blue_coat.jpg');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final ai = CkAi();

  ai.provider = 'openai';

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

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

  final askParams = CkJsonObject();
  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 {
    ai.ask('image');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Get the image response data.;
  try {
    ai.getOutputBd(bdImageData);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

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

  print('Success.');

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

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