All posts

Generating images via API with gpt-image-2

· Tutorial· Images· API

Image generation has the same OpenAI-compatible shape as chat: one endpoint, a prompt, and you get an image back. gpt-image-2 generates high-resolution images (up to 3840×2160) and is billed per image rather than per token, which makes cost easy to predict.

Generate an image

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["TURILOOP_API_KEY"],
    base_url="https://api.turiloop.com/v1",
)

img = client.images.generate(
    model="gpt-image-2",
    prompt="A misty mountain lake at sunrise, photorealistic",
    size="3840x2160",
    n=1,
)
url = img.data[0].url

Or with raw curl:

curl https://api.turiloop.com/v1/images/generations \
  -H "Authorization: Bearer $TURILOOP_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "gpt-image-2", "prompt": "a neon city at night", "size": "3840x2160"}'

Edit an existing image

gpt-image-2 also supports image edits. Pass an input image plus a prompt:

result = client.images.edit(
    model="gpt-image-2",
    image=open("input.png", "rb"),
    prompt="Make it winter, add snow",
    size="3840x2160",
)
edited_url = result.data[0].url

Pricing: per image, not per token

gpt-image-2 is billed per generation, a flat rate per image on Turiloop, the same across 1024, 2K and 4K sizes. That makes budgeting simple: no token math, no size tiers. See the models page for the current rate.

Tips for good results

  • Be specific: subject, style, lighting, composition. "photorealistic", "studio lighting", and "wide shot" all steer the output.
  • Generate at the size you need: there's no quality penalty for 4K at the same price, but a smaller image is faster to download.
  • Iterate with edits: generate once, then refine with images.edit instead of re-rolling from scratch.

Image generation sits behind the same OpenAI-compatible key as the text models on Turiloop. International card, pay-as-you-go, no Chinese phone number.