Prompt
image generation sdk python
Latest observation
Oct 11, 2026
OpenAI APIWeb search: off
If you want a Python SDK for image generation, the best option depends on which model/provider you want to use.
Common choices
1. OpenAI Python SDK
Good if you want to generate images with OpenAI models.
Install:
pip install openai
Example:
from openai import OpenAI
client = OpenAI()
result = client.images.generate(
model="gpt-image-1",
prompt="A futuristic city at sunset, ultra detailed"
)
image_url = result.data[0].url
print(image_url)
If the response is base64 instead of a URL, you can save it like this:
import base64
image_b64 = result.data[0].b64_json
with open("image.png", "wb") as f:
f.write(base64.b64decode(image_b64))
2. Stability AI SDK / API
Good for Stable Diffusion-based generation.
Typical flow:
pip install stability-sdk
Then use their API/client depending on your setup.
3. Hugging Face Diffusers
Good if you want local or self-hosted generation.
Install:
pip install diffusers transformers accelerate torch
Example:
from diffusers import StableDiffusionPipeline
import torch
pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16
).to("cuda")
image = pipe("A futuristic city at sunset").images[0]
image.save("output.png")
Quick recommendation
- OpenAI SDK → easiest cloud API
- Diffusers → local/self-hosted
- Stability AI → another cloud image generation API
If you want, I can give you:
- a complete OpenAI Python example,
- a FastAPI wrapper, or
- a local Stable Diffusion script.