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

Lua
local chilkat = require("chilkat")

-- 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.
local bdImageData = chilkat.BinData.new()
local success = bdImageData:load_file("qa_data/jpg/kid_blue_coat.jpg")
if not success then
    print(bdImageData.last_error_text)
    return
end

local ai = chilkat.Ai.new()

ai.provider = "openai"

-- Use your provider's API key.
ai.api_key = "MY_API_KEY"

-- Choose a model.
ai.model = "gpt-5.6-terra"

local askParams = chilkat.JsonObject.new()
askParams:update_string("image.output_format", "jpeg")
ai:set_ask_params(askParams)

ai:input_add_image_data(bdImageData, "")
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.idle_timeout_ms = 120000

-- Ask the AI for image output.
success = ai:ask("image")
if not success then
    print(ai.last_error_text)
    return
end

-- Get the image response data.;
success = ai:get_output_bd(bdImageData)
if not success then
    print(ai.last_error_text)
    return
end

bdImageData:write_file("c:/aaworkarea/out.jpg")

print("Success.")

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

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