Java Requires Chilkat v11.4.0+
Chilkat2 Java
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 Java Downloads
import com.chilkatsoft.chilkat2.Ai;
import com.chilkatsoft.chilkat2.BinData;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.JsonObject;
public class ChilkatExample {
public static void main(String[] args) {
// 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.
BinData bdImageData = new BinData();
try {
bdImageData.loadFile("qa_data/jpg/kid_blue_coat.jpg");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
Ai ai = new Ai();
ai.setProvider("openai");
// Use your provider's API key.
ai.setApiKey("MY_API_KEY");
// Choose a model.
ai.setModel("gpt-5.6-terra");
JsonObject 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.setIdleTimeoutMs(120000);
// Ask the AI for image output.
try {
ai.ask("image");
// Get the image response data.;
ai.getOutputBd(bdImageData);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
bdImageData.writeFile("c:/aaworkarea/out.jpg");
System.out.println("Success.");
// -------------------------------
// Sample Input:
// -------------------------------
//
// -------------------------------
// Sample Output:
// -------------------------------
//
}
}