56 lines
2.6 KiB
Python
Executable File
56 lines
2.6 KiB
Python
Executable File
"""Codex CLI의 내장 이미지 생성 도구로 그림 한 장을 만들어 PNG로 저장한다 (날씨각 sns/admin codex-image.mjs와 같은 방식).
|
|
|
|
usage: python3 codex_image.py --prompt-file p.txt --output out.png [--image ref.jpg ...]
|
|
"""
|
|
import argparse
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
# 도구 호출·저장만 하므로 중간 모델·낮은 추론으로 고정한다 (sns/admin과 같은 이유)
|
|
IMAGE_MODEL = os.environ.get("CODEX_IMAGE_MODEL", "gpt-6-sol")
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
|
parser.add_argument("--prompt-file", required=True)
|
|
parser.add_argument("--output", required=True)
|
|
parser.add_argument("--image", action="append", default=[], help="참고 이미지 (여러 번 줄 수 있다)")
|
|
args = parser.parse_args()
|
|
|
|
work = Path(tempfile.mkdtemp(prefix="codex-image-"))
|
|
task = "\n".join([
|
|
"내장 이미지 생성 도구로 아래 프롬프트대로 이미지를 만든다."
|
|
+ (" 첨부한 참고 이미지는 프롬프트가 말한 용도로만 따른다." if args.image else ""),
|
|
"만든 이미지를 직접 열어 확인한다: 프롬프트와 다르거나 손·얼굴이 일그러졌거나 글자·로고가 있으면 한 번만 다시 만든다 (최대 2장).",
|
|
"통과한 한 장만 이 폴더에 result.png 로 저장한다. 다른 파일은 만들지 않는다.",
|
|
"",
|
|
Path(args.prompt_file).read_text(encoding="utf-8").strip(),
|
|
])
|
|
command = ["codex", "exec", "--skip-git-repo-check", "-C", str(work), "-s", "workspace-write", "-m", IMAGE_MODEL,
|
|
"-c", 'model_reasoning_effort="low"']
|
|
for image in args.image:
|
|
command += ["-i", str(Path(image).resolve())]
|
|
try:
|
|
subprocess.run(command, input=task, text=True, stdout=subprocess.DEVNULL, check=True)
|
|
result = work / "result.png"
|
|
if not result.is_file():
|
|
pngs = sorted(work.glob("*.png"))
|
|
if not pngs:
|
|
sys.exit(f"Codex가 그림을 저장하지 않았어요: {work}")
|
|
result = pngs[0]
|
|
Path(args.output).parent.mkdir(parents=True, exist_ok=True)
|
|
shutil.copyfile(result, args.output)
|
|
print("saved", args.output)
|
|
except subprocess.CalledProcessError as error:
|
|
sys.exit(f"Codex 실행이 실패했어요 ({error}). 사용 한도나 로그인을 확인하세요.")
|
|
finally:
|
|
shutil.rmtree(work, ignore_errors=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|