Sample code for 30+ languages & platforms
Tcl

Simple AI Image Generation

See more AI Examples

Create an image by providing a text description.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

# This example assumes the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.

set ai [new_CkAi]

CkAi_put_Provider $ai "openai"

# Use your provider's API key.
CkAi_put_ApiKey $ai "MY_API_KEY"

# Choose a model.
CkAi_put_Model $ai "gpt-5"

set askParams [new_CkJsonObject]

CkJsonObject_UpdateString $askParams "image.output_format" "jpeg"
CkJsonObject_UpdateString $askParams "image.size" "1024x1024"
CkJsonObject_UpdateString $askParams "image.quality" "low"
CkAi_SetAskParams $ai $askParams

CkAi_InputAddText $ai "Generate a small, cute illustration of a gray tabby cat hugging a happy otter wearing an orange scarf"

# Ask the AI for image output.
set success [CkAi_Ask $ai "image"]
if {$success == 0} then {
    puts [CkAi_lastErrorText $ai]
    delete_CkAi $ai
    delete_CkJsonObject $askParams
    exit
}

# Get the image response data.
set bdImageData [new_CkBinData]

set success [CkAi_GetOutputBd $ai $bdImageData]
if {$success == 0} then {
    puts [CkAi_lastErrorText $ai]
    delete_CkAi $ai
    delete_CkJsonObject $askParams
    delete_CkBinData $bdImageData
    exit
}

CkBinData_WriteFile $bdImageData "c:/aaworkarea/out.jpg"

puts "Success."

delete_CkAi $ai
delete_CkJsonObject $askParams
delete_CkBinData $bdImageData