# Category image batch generator (ASCII-safe, PS5 compatible) $ErrorActionPreference = 'Continue' $srcFile = "$PSScriptRoot\categories.txt" $mapFile = "$PSScriptRoot\map.json" $outDir = "$PSScriptRoot\downloaded" $idxFile = "$PSScriptRoot\gen_index.json" if (-not (Test-Path $outDir)) { New-Item -ItemType Directory -Path $outDir -Force | Out-Null } $map = Get-Content $mapFile -Raw -Encoding UTF8 | ConvertFrom-Json # PS5 compatible: ConvertFrom-Json returns PSCustomObject, use .PSObject.Properties to check keys $processed = @{} if (Test-Path $idxFile) { $rawIdx = Get-Content $idxFile -Raw -Encoding UTF8 if ($rawIdx -and $rawIdx.Trim() -ne '{}') { $objIdx = $rawIdx | ConvertFrom-Json foreach ($p in $objIdx.PSObject.Properties) { $processed[$p.Name] = $p.Value } } } $NO_TEXT = ", no text, no watermark, no logo, no signature, no artist tag, no caption, no brand mark, no subtitle, no embedded letters, no corner badge, no AI generated label, no copyright mark, no overlay text" function Get-VegeFruitPrompt($name) { return "a bamboo basket full of fresh $name, placed on the soil of a $name farm or orchard field, background with rows of naturally growing $name plants or trees, natural daylight, bright fresh mint green mood, realistic photography style, close-up composition, 1:1 square$NO_TEXT" } function Get-AnimalLivePrompt($animalEn) { return "a herd of $animalEn grazing or standing naturally on a green grass pasture or farmhouse courtyard, background with distant hills and blue sky, natural daylight, bright fresh mint green mood, realistic photography style, wide shot, 1:1 square composition$NO_TEXT" } function Get-EggPrompt($enName) { return "a bamboo basket full of fresh $enName with some green plants and straw decoration, placed on a rustic wooden table in a sunny farmhouse courtyard, natural daylight, warm pastoral mood with fresh mint green undertone, realistic photography, 1:1 square$NO_TEXT" } function Get-WaterPrompt { return "a natural fish pond with various live fish swimming in clear blue water, surrounded by green grass and reed, natural daylight, fresh mint green mood, realistic photography, 1:1 square composition$NO_TEXT" } function Get-ProcessedPrompt($enName) { return "fresh $enName arranged nicely on a rustic wooden table, background with farmhouse kitchen or market stall setting, natural daylight, fresh mint green mood, realistic photography style, close-up, 1:1 square$NO_TEXT" } # helper: get vege en hint function Get-VegeEn($name) { foreach ($prop in $map.vege_en_hint.PSObject.Properties) { if ($prop.Name -eq $name) { return $prop.Value } } return $name } function Get-LiveAnimalEn($name) { foreach ($prop in $map.live_animal_map.PSObject.Properties) { if ($prop.Name -eq $name) { return $prop.Value } } return $null } $lines = Get-Content $srcFile -Encoding UTF8 | Where-Object { $_ -match '^\d+\t' } $total = ($lines | Where-Object { ($_ -split "`t")[1] -ne '0' }).Count Write-Output "TOTAL_SECONDARY=$total" $done = 0; $fail = 0 foreach ($line in $lines) { $parts = $line -split "`t" if ($parts.Count -lt 4) { continue } $id = [int]$parts[0] $parentId = [int]$parts[1] $name = $parts[2].Trim() $kind = $parts[3].Trim() if ($parentId -eq 0) { continue } $key = "$id" if ($processed.ContainsKey($key)) { $done++; continue } if ($kind -eq 'farm') { $en = Get-VegeEn $name $prompt = Get-VegeFruitPrompt $en } else { $animalEn = Get-LiveAnimalEn $name if ($null -ne $animalEn) { $prompt = Get-AnimalLivePrompt $animalEn } elseif ($map.egg_list -contains $name) { $en = Get-VegeEn $name $prompt = Get-EggPrompt $en } elseif ($map.water_list -contains $name) { $prompt = Get-WaterPrompt } elseif ($map.processed_list -contains $name) { $en = Get-VegeEn $name $prompt = Get-ProcessedPrompt $en } else { $en = Get-VegeEn $name $prompt = Get-VegeFruitPrompt $en } } $outFile = "$outDir\$id.jpg" $body = @{prompt = $prompt; image_size = "square_hd"} | ConvertTo-Json -Compress try { Invoke-WebRequest -Uri "https://trae-api-cn.mchost.guru/api/ide/v1/text_to_image" ` -Method POST -ContentType "application/json" -Body $body ` -OutFile $outFile -TimeoutSec 120 if (-not (Test-Path $outFile)) { throw "文件未生成: $outFile" } $sz = (Get-Item $outFile).Length if ($sz -lt 10000) { throw "图片太小 size=$sz" } $processed[$key] = @{ name=$name; pid=$parentId; file="$outFile"; size=$sz } $processed | ConvertTo-Json -Depth 5 | Set-Content $idxFile -Encoding UTF8 $done++ if ($done % 20 -eq 0) { Write-Output "PROGRESS $done / $total (last: $name id=$id)" } } catch { $fail++ Write-Output "FAIL id=$id name=$name err=$($_.Exception.Message)" Start-Sleep -Seconds 3 } Start-Sleep -Milliseconds 300 } Write-Output "DONE success=$done fail=$fail total=$total"