Photorealistic Image Generation with GPT Image 2 Serverless API

Generate Stunning Photorealistic Images from Descriptive Text Prompts using GPT Image 2.

 1import requests
 2import time
 3import json
 4
 5api_key = "YOUR_API_KEY"
 6url = "https://api.segmind.com/workflows/69f64e665456d2c942098b3a-v1"
 7
 8data = {
 9    "text_2rpv9": "A photorealistic, cinematic shot of a cozy independent bookstore in Mumbai at golden hour. Warm afternoon sunlight streams through a tall front window onto wooden shelves packed with books; book spines are clearly visible with titles in English and Hindi Devanagari script. In the foreground, a handwritten chalkboard A-frame easel reads, in clean legible chalk lettering: first line 'मुंबई पुस्तक भंडार', second line 'Mumbai Book Store', and a smaller third line 'Open Daily  9 am – 9 pm'. Shallow depth of field, shot on a full-frame camera at 35mm f/2.0, ultra-realistic detail, natural color grading, dust motes in sunlight, film grain."
10}
11
12def poll_for_result(poll_url):
13    """Poll the API until the generation is complete"""
14    while True:
15        response = requests.get(
16            poll_url,
17            headers={'Authorization': f'Bearer {api_key}'}
18        )
19        result = response.json()
20
21        if result['status'] == 'COMPLETED':
22            # Parse the output (it's a JSON string)
23            outputs = json.loads(result['output'])
24            return outputs
25        elif result['status'] == 'FAILED':
26            raise Exception(result.get('error', 'Generation failed'))
27
28        # Wait 7 seconds before polling again
29        time.sleep(7)
30
31# Make the initial request
32response = requests.post(
33    url,
34    json=data,
35    headers={
36        'Authorization': f'Bearer {api_key}',
37        'Content-Type': 'application/json'
38    }
39)
40
41if response.status_code == 200:
42    result = response.json()
43    print('Request queued:', result)
44
45    # Start polling for results
46    outputs = poll_for_result(result['poll_url'])
47    print('Generation complete:', outputs)
48else:
49    print(f"Error: {response.status_code}")
50    print(response.text)

API Endpoint

POSThttps://api.segmind.com/workflows/69f64e665456d2c942098b3a-v1

Parameters

text_2rpv9optional
string

Text 2rpv9

Default: "A photorealistic, cinematic shot of a cozy independent bookstore in Mumbai at golden hour. Warm afternoon sunlight streams through a tall front window onto wooden shelves packed with books; book spines are clearly visible with titles in English and Hindi Devanagari script. In the foreground, a handwritten chalkboard A-frame easel reads, in clean legible chalk lettering: first line 'मुंबई पुस्तक भंडार', second line 'Mumbai Book Store', and a smaller third line 'Open Daily 9 am – 9 pm'. Shallow depth of field, shot on a full-frame camera at 35mm f/2.0, ultra-realistic detail, natural color grading, dust motes in sunlight, film grain."

Response Format

Returns: Polling-based asynchronous response

Initial request returns a poll_url. Poll every 7 seconds until status is COMPLETED.

Common Error Codes

The API returns standard HTTP status codes. Detailed error messages are provided in the response body.

400

Bad Request

Invalid parameters or request format

401

Unauthorized

Missing or invalid API key

403

Forbidden

Insufficient permissions

404

Not Found

Workflow not found

406

Insufficient Credits

Not enough credits to process request

429

Rate Limited

Too many requests

500

Server Error

Internal server error

502

Bad Gateway

Service temporarily unavailable

504

Timeout

Request timed out