Sample code for 30+ languages & platforms
Swift

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 Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    // 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 bdImageData = CkoBinData()!
    success = bdImageData.loadFile(path: "qa_data/jpg/kid_blue_coat.jpg")
    if success == false {
        print("\(bdImageData.lastErrorText!)")
        return
    }

    let ai = CkoAi()!

    ai.provider = "openai"

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

    // Choose a model.
    ai.model = "gpt-5"

    let askParams = CkoJsonObject()!
    askParams.updateString(jsonPath: "image.output_format", value: "jpeg")
    ai.setAskParams(json: askParams)

    ai.inputAddImageData(bd: bdImageData, summary: "")
    ai.inputAddText(text: "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.
    success = ai.ask(outputType: "image")
    if success == false {
        print("\(ai.lastErrorText!)")
        return
    }

    // Get the image response data.;
    success = ai.getOutputBd(bd: bdImageData)
    if success == false {
        print("\(ai.lastErrorText!)")
        return
    }

    bdImageData.writeFile(path: "c:/aaworkarea/out.jpg")

    print("Success.")

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

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

}