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

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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 bd_image_data = try chilkat.BinData.init();
    defer bd_image_data.deinit();
    bd_image_data.loadFile("qa_data/jpg/kid_blue_coat.jpg") catch {
        std.debug.print("{s}\n", .{try bd_image_data.getLastErrorText(alloc)});
        return;
    };

    const ai = try chilkat.Ai.init();
    defer ai.deinit();

    ai.setProvider("openai");

    // Use your provider's API key.
    ai.setApiKey("MY_API_KEY");

    // Choose a model.
    ai.setModel("gpt-5.6-terra");

    const ask_params = try chilkat.JsonObject.init();
    defer ask_params.deinit();
    ask_params.updateString("image.output_format", "jpeg") catch {};
    ai.setAskParams(ask_params) catch {};

    ai.inputAddImageData(bd_image_data, "") catch {};
    ai.inputAddText("Modify the image by replacing the blue coat with a Metallica T-shirt.") catch {};

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

    // Ask the AI for image output.
    ai.ask("image") catch {
        std.debug.print("{s}\n", .{try ai.getLastErrorText(alloc)});
        return;
    };

    // Get the image response data.;
    ai.getOutputBd(bd_image_data) catch {
        std.debug.print("{s}\n", .{try ai.getLastErrorText(alloc)});
        return;
    };

    bd_image_data.writeFile("c:/aaworkarea/out.jpg") catch {};

    std.debug.print("Success.\n", .{});

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

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