Skip to content

GPT Image Formatting

Page Overview

  • gpt-4o-image and gpt-4o-image-vip are the new GPT-4o image capabilities in ChatGPT. The OpenAI API model gpt-image-1 provides a variety of powerful image generation features, allowing you to create images from natural language descriptions, and edit or modify images through conversation.
import http.client
import json
from urllib.parse import urlparse # Used to parse URLs
# 1. Configuration parameters (add the API key and endpoint directly in the code)
# ==============================================================================
# Replace "YOUR_ACTUAL_API_KEY" with your real API key.
API_KEY = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Replace with your real key
# Replace "your_api_provider.com" with the actual API host name.
API_BASE_URL_CONFIG = "https://sg.4All API.com" # Example, replace with your base API URL
API_PATH_CONFIG = "/v1/chat/completions" # Specific API path
MODEL = "gpt-image-1" # The model you want to use, such as gpt-4o-image, gpt-4o-image-vip, gpt-image-1, dall-e-3
# Parameters for image editing
PROMPT_TEXT = "Modify this image to make it look more cartoonish and more vibrant in color." # Change to the editing instruction you want
IMAGE_URL_TO_EDIT = "https://github.com/dianping/cat/raw/master/cat-home/src/main/webapp/images/logo/cat_logo03.png"
# ==============================================================================
def call_image_api(api_key, base_url_config, path_config, model, prompt_text, image_url):
"""
Call the image processing API.
Parameters:
api_key (str): API key.
base_url_config (str): The API base URL or host name.
path_config (str): The API path.
model (str): The model name to use.
prompt_text (str): Text prompt describing the operation.
image_url (str): URL of the image to process.
Returns:
dict or None: Parsed JSON response, or None if the call fails.
"""
parsed_url = urlparse(base_url_config)
host = parsed_url.netloc if parsed_url.netloc else parsed_url.path # If there is no scheme, netloc is empty and path is the host name
scheme = parsed_url.scheme if parsed_url.scheme else "https" # Use https by default
if not host:
print(f"Error: Unable to parse host name from '{base_url_config}'.")
return None
print(f"Preparing to connect to: {scheme}://{host}{path_config}")
# Build the request payload
payload_dict = {
"model": model,
"stream": False, # For image editing or generation, streaming responses are usually not needed
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt_text
},
{
"type": "image_url",
"image_url": {
"url": image_url
}
}
]
}
]
# Depending on your API docs, you may also need other parameters such as max_tokens, temperature, etc.
# If the API is for generation rather than editing, the payload structure may differ (for example, using "prompt" directly instead of "messages")
}
payload_json = json.dumps(payload_dict)
# Set request headers
headers = {
'Accept': 'application/json', # APIs usually return JSON
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json'
}
conn = None
try:
if scheme == "https":
conn = http.client.HTTPSConnection(host)
elif scheme == "http":
conn = http.client.HTTPConnection(host)
else:
print(f"Error: Unsupported scheme '{scheme}'. Please use 'http' or 'https'.")
return None
print(f"Sending request to POST {path_config}...")
# print(f"Payload: {payload_json}") # Uncomment to print the payload during debugging
conn.request("POST", path_config, payload_json, headers)
res = conn.getresponse()
response_status = res.status
response_reason = res.reason
response_data_bytes = res.read()
print(f"Received response: {response_status} {response_reason}")
if response_status == 200:
try:
decoded_data = response_data_bytes.decode("utf-8")
json_response = json.loads(decoded_data)
print("API call succeeded. Response content:")
print(json.dumps(json_response, indent=2, ensure_ascii=False)) # Pretty-print JSON
return json_response
except json.JSONDecodeError:
print("Error: Unable to decode response as JSON. Raw response content:")
print(response_data_bytes.decode("utf-8", errors="replace")) # Try to decode and replace undecodable characters
return None
except Exception as e:
print(f"Error while processing successful response: {e}")
return None
else:
print("API call failed.")
print(f"Raw response content ({response_status} {response_reason}):")
print(response_data_bytes.decode("utf-8", errors="replace"))
return None
except http.client.Gaierror as e: # Address resolution error
print(f"Error: Unable to resolve host name '{host}'. Please check the API base URL configuration. Details: {e}")
except ConnectionRefusedError as e:
print(f"Error: Connection to '{host}' was refused. Please check whether the API service is running and your network connection. Details: {e}")
except http.client.HTTPException as e: # More generic HTTP client error
print(f"An error occurred during the HTTP request: {e}")
except Exception as e:
print(f"An unknown error occurred: {e}")
import traceback
traceback.print_exc() # Print detailed stack trace
finally:
if conn:
conn.close()
# print("Connection closed.")
return None
# Main program entry point
if __name__ == "__main__":
if API_KEY == "YOUR_ACTUAL_API_KEY" or not API_KEY: # Check again in case the user forgot to replace it
print("Error: Please replace 'YOUR_ACTUAL_API_KEY' at the top of the script with your real API key.")
else:
# Call the API function
result = call_image_api(
api_key=API_KEY,
base_url_config=API_BASE_URL_CONFIG,
path_config=API_PATH_CONFIG,
model=MODEL,
prompt_text=PROMPT_TEXT,
image_url=IMAGE_URL_TO_EDIT
)
if result:
# Here, you can process 'result' based on the API's actual response structure
# For example, if it returns the edited image URL or base64 data:
# edited_image_url = result.get("choices", [{}])[0].get("message", {}).get("tool_calls", [{}])[0].get("image",{}).get("url")
# if edited_image_url:
# print(f"\nEdited image URL (example extraction path): {edited_image_url}")
# else:
# print("\nUnable to extract the edited image URL from the response. Please check the complete response above.")
pass # This code currently only prints the full response; the exact extraction logic depends on the API's JSON structure
else:
print("\nThe API call did not return valid data.")
print("\nScript execution complete.")
  • Replace the key in API_KEY = "sk-wLdqZ6SP5GjJSxHwsUbFNUVKMxhDJCoG06Xfexo2V9Eq4Z6n" at the top of the script with your real API key obtained from sg.4All API.com (or another provider).
  • Modify API_BASE_URL_CONFIG as needed (default: https://sg.4All API.com) and API_PATH_CONFIG (default: /v1/chat/completions) to match your API service.
  • Modify MODEL, PROMPT_TEXT, and IMAGE_URL_TO_EDIT as needed.
  • Run the script.

Please note that after a successful API call in the if __name__ == "__main__": section, the current code only prints the full JSON response. You will need to further parse and extract the information you need—such as the edited image URL or base64 data—based on the actual JSON structure returned by your API (gpt-4o-image model on sg.4All API.com). I have commented out example extraction code at the end of the script; you will need to adjust it according to your actual response format.