Rust Requires Chilkat v11.4.0+
Rust
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 Rust Downloads
// 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.
let bd_image_data = chilkat::BinData::new();
if bd_image_data.load_file("qa_data/jpg/kid_blue_coat.jpg").is_err() {
println!("{}", bd_image_data.last_error_text());
return;
}
let ai = chilkat::Ai::new();
ai.set_provider("openai");
// Use your provider's API key.
ai.set_api_key("MY_API_KEY");
// Choose a model.
ai.set_model("gpt-5.6-terra");
let ask_params = chilkat::JsonObject::new();
let _ = ask_params.update_string("image.output_format", "jpeg");
let _ = ai.set_ask_params(&ask_params);
let _ = ai.input_add_image_data(&bd_image_data, "");
let _ = ai.input_add_text("Modify the image by replacing the blue coat with a Metallica T-shirt.");
// Give the AI some time (2 minutes).
ai.set_idle_timeout_ms(120000);
// Ask the AI for image output.
if ai.ask("image").is_err() {
println!("{}", ai.last_error_text());
return;
}
// Get the image response data.;
if ai.get_output_bd(&bd_image_data).is_err() {
println!("{}", ai.last_error_text());
return;
}
let _ = bd_image_data.write_file("c:/aaworkarea/out.jpg");
println!("Success.");
// -------------------------------
// Sample Input:
// -------------------------------
//
// -------------------------------
// Sample Output:
// -------------------------------
//