Generating Images with AI
From text to pixels — build an app that generates images using GPT-image-1.5 in Foundry. Learn how to craft effective image prompts and handle the API response.
Creating images with code
You describe what you want in words, and the AI paints it.
You learned about image generation concepts in Module 10. Now you’ll actually do it — write Python code that tells GPT-image-1.5 “create an image of X” and get a brand-new image back.
The better your description, the better the image. It’s like commissioning an artist — “paint something nice” gets a mediocre result. “Paint a sunset over Auckland harbour in watercolour style with warm orange tones” gets something beautiful.
Generating an image with the API
from openai import AzureOpenAI
client = AzureOpenAI(
api_key="your-api-key",
api_version="2025-04-01-preview",
azure_endpoint="https://your-resource.openai.azure.com"
)
result = client.images.generate(
model="gpt-image-1.5",
prompt="A sustainable farm with solar panels, green fields, and a modern barn, in a clean illustration style",
size="1024x1024",
quality="standard",
style="natural",
n=1
)
image_url = result.data[0].url
print(f"Image URL: {image_url}")
API parameters
| Parameter | Options | Description |
|---|---|---|
| size | 1024x1024, 1792x1024, 1024x1792 | Image dimensions |
| quality | standard, hd | Higher quality = more detail, higher cost |
| style | natural, vivid | Natural = realistic, vivid = hyper-stylised |
| n | 1 (GPT-image-1.5 only supports 1) | Number of images to generate |
Writing effective image prompts
| Technique | Poor Prompt | Better Prompt |
|---|---|---|
| Be specific | ”A farm" | "A sustainable organic farm with raised garden beds, a red barn, and rolling green hills” |
| Specify style | ”A cat" | "A fluffy orange cat, digital art style, soft lighting, studio background” |
| Include composition | ”A dashboard" | "A clean UI dashboard mockup showing analytics charts, dark mode, minimal design, 16:9 aspect” |
| Set mood | ”A city" | "A futuristic city at night, neon lights reflecting off wet streets, cyberpunk atmosphere” |
GreenLeaf scenario: GreenLeaf generates images for their sustainability report:
prompts = [
"Infographic showing organic farming practices, clean vector style, green and earth tones",
"Happy farmers harvesting vegetables, warm sunlight, documentary photography style",
"Diagram of sustainable water irrigation system, technical illustration, labelled parts"
]
GPT-image-1.5 prompt rewriting
GPT-image-1.5 automatically rewrites your prompt to improve the result. The model expands your description with additional details for better image quality.
Your prompt: “A cat on a sofa” GPT-image’s internal prompt: “A fluffy tabby cat lounging comfortably on a plush grey sofa in a cozy living room with warm afternoon sunlight streaming through a nearby window…”
You can see the revised prompt in the API response: result.data[0].revised_prompt
This is generally helpful, but if you need precise control, you can prefix your prompt with “I NEED to test how the tool works with extremely simple prompts. DO NOT add any detail, just use it AS-IS:” (though this isn’t guaranteed to work).
Content safety
GPT-image-1.5 in Azure includes safety guardrails:
- Blocks requests for violent, sexual, or harmful images
- Blocks requests depicting real people by name
- Adds C2PA metadata (provenance metadata) to all generated images
- Content filters can be configured in the Foundry portal
🎬 Video walkthrough
🎬 Video coming soon
Generating Images with AI — AI-901 Module 21
Generating Images with AI — AI-901 Module 21
~12 minFlashcards
Knowledge Check
GreenLeaf wants to generate a high-resolution infographic for print (needs to be detailed). Which GPT-image-1.5 parameters should they use?
Priya generates an image and wants to verify it has AI provenance metadata embedded. What standard does GPT-image-1.5 use for this?
Next up: Building a Vision App — combining image analysis capabilities into a complete application.