← Back to Blog
Image Generation APIAI CodingCodeGatewayGPT-Image-2

GPT-Image-2 API Quickstart: Generate Images with Python in 5 Minutes

May 12, 2026
GPT-Image-2 API Quickstart: Generate Images with Python in 5 Minutes

TL;DR

  • GPT-Image-2 supports up to 4K resolution, transparent PNG output, and image inpainting.
  • Connect via CodeGateway: base_url="https://api.codegateway.dev/v1" with your standard OpenAI SDK. Five lines to early image.
  • strong for: logos with embedded text, transparent product cutouts, and editing existing images.

GPT-Image-2 Specs

Spec

Value

Resolutions

1024×1024, 1536×1024, 1024×1536 (1K); 2048×2048 (2K); 4096×4096 (4K)

Output formats

JPEG, PNG, WebP

Transparent background

Supported (PNG/WebP)

Image editing (inpainting)

Supported

Batch generation

Up to 10 per request

Official pricing

$0.011/image (low quality 1K) to $0.167/image (high quality 4K)

Source: OpenAI API documentation (May 2026). Check OpenAI's pricing page for current rates.

CodeGateway multiplies official prices by the applicable tier rate (1.2x–1.5x). See CodeGateway pricing tiers.

5-Minute Python Quickstart

bash
pip install openai

Basic generation

python
from openai import OpenAI

client = OpenAI(
api_key="your-codegateway-api-key",
base_url="https://api.codegateway.dev/v1"
)

response = client.images.generate(
model="gpt-image-2",
prompt="An orange cat sitting on a stack of books, reading under warm lamplight, watercolor illustration style",
n=1,
size="1024x1024"
)

image_url = response.data[0].url
print(f"Image URL: {image_url}") # Valid for 60 minutes

Save to disk (base64)

python
import base64

response = client.images.generate(
model="gpt-image-2",
prompt="Minimalist app UI screenshot, blue theme, clean white background",
n=1,
size="1024x1024",
quality="high",
output_format="png"
)

img_bytes = base64.b64decode(response.data[0].b64_json)
with open("output.png", "wb") as f:
f.write(img_bytes)
print("Saved to output.png")

Transparent background (for UI assets, stickers)

python
response = client.images.generate(
model="gpt-image-2",
prompt="Clean app icon representing a code gateway, deep blue, logo style",
n=1,
size="1024x1024",
quality="high",
output_format="png",
background="transparent" # requires PNG or WebP
)

Parameter Reference

size

Value

Resolution

strong for

1024x1024

1K square

Social media, avatars, product images

1536x1024

1K landscape

Blog headers, website banners

1024x1536

1K portrait

Phone wallpapers, posters

2048x2048

2K square

Print-quality illustrations

4096x4096

4K square

Ultra-high resolution (high cost)

quality

Value

Characteristics

Use when

low

Fast, less detail

Drafts, rapid iteration

medium

Default, balanced

General commercial use

high

Highest detail, ~20–30s

Final product images, detailed illustrations

Cost by quality tier (1024×1024, via CodeGateway)

Quality

Official price

CodeGateway (new user 1.5x)

low

$0.011/image

$0.017

medium

$0.042/image

$0.063

high

$0.080/image

$0.120

Generating 100 medium-quality images per day: ~$190/month at new-user rate. Multiplier drops as cumulative spend grows — see tier pricing.

Image Editing (Inpainting)

GPT-Image-2 supports text-guided inpainting — modify part of an existing image while keeping the rest intact.

python
import base64

with open("original.png", "rb") as f:
original_b64 = base64.b64encode(f.read()).decode()

with open("mask.png", "rb") as f:
mask_b64 = base64.b64encode(f.read()).decode()

response = client.images.edit(
model="gpt-image-2",
image=original_b64,
mask=mask_b64, # White = areas to replace; Black = keep as-is
prompt="Replace the background with a sunset beach scene",
n=1,
size="1024x1024"
)

Mask format: PNG, same dimensions as the original. White pixels mark areas to regenerate; black pixels preserve the original.

Batch Generation (up to 10 images)

python
response = client.images.generate(
model="gpt-image-2",
prompt="Data visualization dashboard, neon blue glow effect, dark background, tech aesthetic",
n=4, # 4 variations in one API call
size="1024x1024",
quality="medium"
)

for i, img in enumerate(response.data):
print(f"Variant {i+1}: {img.url}")

GPT-Image-2 vs Imagen 4: Which to Use?

Dimension

GPT-Image-2

Imagen 4

Text rendering

Strong (accurate logo/poster text)

Weaker

Photorealism

High

Very high (Imagen 4 Ultra)

Artistic style range

Wide

Wide

Transparent background

Supported

Not supported

Image editing

Supported (inpainting)

Not supported (Standard/Fast)

Lowest cost entry

$0.011/image (low quality)

Similar at Fast tier

When to pick which:

  • Logos, posters, or any image with readable text → GPT-Image-2
  • Ultra-realistic people/scenes → Imagen 4 Ultra
  • Edit an existing image (inpainting) → GPT-Image-2
  • Stylized illustration with no text → comparable; choose by cost or personal preference

Full comparison: Image Generation API Comparison: Imagen, Gemini, and GPT-Image.

FAQ

Q: How long are generated image URLs valid?

A: A: 60 minutes. For persistent storage, request response_format="b64_json" and store the bytes yourself.

Q: Does GPT-Image-2 support non-English prompts?

A: A: Yes, including Chinese and most major languages. English prompts tend to give more precise control over stylistic nuances.

Q: How fast is generation?

A: A: Low quality: 5–10s. Medium: 10–20s. High: 20–40s. Server load and network conditions vary.

Q: Can it generate faces?

A: A: Yes, with OpenAI's standard content policy applied. Generating likenesses of specific real people is rejected.

Q: Can I reuse an OpenAI API key with CodeGateway?

A: A: No — CodeGateway API keys work only with base_url="https://api.codegateway.dev/v1". They're separate credentials.

AuthorCodeGateway TeamReviewed on2026-05-16
GPT-Image-2 API Quickstart: Python Image Generation Guide