Files
image-gen-custom-skill/skill/image-gen-custom/scripts/generate-image.ps1
T
2026-09-05 21:08:13 +08:00

37 lines
1.6 KiB
PowerShell

param(
[Parameter(Mandatory = $true)][string] $Prompt,
[string] $Out = "outputs\imagegen.png",
[string] $Size = "1024x1024",
[ValidateSet("low", "medium", "high", "auto")][string] $Quality = "low",
[string] $Model = "gpt-image-2",
[string] $BaseUrl = "",
[switch] $Force
)
$ErrorActionPreference = "Stop"
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$pythonSelector = Join-Path $scriptDir "runtime.py"
$generator = Join-Path $scriptDir "generate_image.py"
function Get-ImageGenPython {
if ($env:CODEX_IMAGEGEN_PYTHON) {
$command = Get-Command $env:CODEX_IMAGEGEN_PYTHON -ErrorAction SilentlyContinue
if ($command) { return $command.Source }
}
$embedded = Join-Path $env:USERPROFILE ".cache\codex-runtimes\codex-primary-runtime\dependencies\python\python.exe"
if (Test-Path -LiteralPath $embedded) { return $embedded }
$command = Get-Command python.exe -ErrorAction SilentlyContinue | Select-Object -First 1
if ($command) { return $command.Source }
$command = Get-Command py.exe -ErrorAction SilentlyContinue | Select-Object -First 1
if ($command) { return $command.Source }
throw "Python 3 not found; install Python or set CODEX_IMAGEGEN_PYTHON."
}
$python = Get-ImageGenPython
$runtime = & $python $pythonSelector
if ($LASTEXITCODE -ne 0 -or -not $runtime) { throw "Could not bootstrap Python runtime." }
$args = @($generator, "--model", $Model, "--prompt", $Prompt, "--size", $Size, "--quality", $Quality, "--out", $Out)
if ($BaseUrl) { $args += @("--base-url", $BaseUrl) }
if ($Force) { $args += "--force" }
& $runtime @args
if ($LASTEXITCODE -ne 0) { throw "Image generation failed with exit code $LASTEXITCODE." }