Skip to main content

Generate images from text

The platform’s raw graph model supports two main usages: The first is to directly generate images by the model only through prompt input; the second is to realize variant creation of images based on existing pictures and combined with new prompts.
  • Generate images based on text descriptions
In order to improve the accuracy and effect of image generation, it is recommended to pay attention to the following points when entering the prompt:
  • Be descriptive: Describe the scene you want clearly and in detail. For example, if you want to generate a beach sunset scene, instead of just entering “beach sunset”, you might as well say “On a quiet beach, the sunset dyes the sky red, the waves lap on the beach, and boats can be seen in the distance.”
  • Mood and atmosphere: You can add words such as “warmth”, “mysterious” or “energetic” to the prompt to help the model grasp the overall feeling or style you want to present.
  • Specify the style: If you have a specific art style requirement, you can specify it directly, such as “Impressionism” or “Surrealism” to improve consistency with expectations.
  • Avoid vague expressions: Try not to use subjective or vague words such as “beautiful” and “good-looking”. Such descriptions are difficult to convert into specific pictures.
  • Use exclusion statements: If you don’t want certain elements to appear, you can clearly indicate the exception in the prompt, such as “No boats.”
  • Staged description: For complex content, it is recommended to generate a basic picture first, and then gradually improve the final image through detailed adjustments.
  • Diverse descriptions: Try different wording or expression angles. Sometimes different descriptions of the same scene will produce different visual effects.
  • Make good use of model parameters and functions: Make good use of the resolution adjustment, style weight setting and other functions provided by the model to further improve image quality and style fit.
from openai import OpenAI

client = OpenAI(api_key="API_KEY", base_url="https://BASE_URL/v1")

response = client.images.generate(
model="gpt-image-1",
prompt="a cat",
size="1024x1024",
n=1
)

print(response)

Edit picture

Models that support reference image editing (image2image) are basically adapted to the OpenAI editing interface.
import base64
from openai import OpenAI
client = OpenAI(api_key="API_KEY", base_url="https://BASE_URL/v1")

prompt = "prompt"

result = client.images.edit(
model="gpt-image-1",
image=[
open("image_path.png", "rb"),
    ],
prompt=prompt
)

image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)

# Save the image to a file
with open("image_path.png", "wb") as f:
f.write(image_bytes)

Things to note

  1. When using, you need toOPENAI_BASE_URLset tohttps://BASE_URL/v1
  2. OPENAI_API_KEYshould be set to your API Key
  3. Most models have been adapted to the OpenAI mapping interface. Some models have not been adapted. Please refer to the model documentation.