Domain 2 β€” Module 10 of 15 67%
21 of 26 overall
Domain 2: Implement AI Solutions Using Foundry Free ⏱ ~12 min read

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

Simple explanation

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

ParameterOptionsDescription
size1024x1024, 1792x1024, 1024x1792Image dimensions
qualitystandard, hdHigher quality = more detail, higher cost
stylenatural, vividNatural = realistic, vivid = hyper-stylised
n1 (GPT-image-1.5 only supports 1)Number of images to generate

Writing effective image prompts

TechniquePoor PromptBetter 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

Flashcards

Question

What API parameters can you control when generating images with GPT-image-1.5?

Click or press Enter to reveal answer

Answer

size (1024x1024, 1792x1024, 1024x1792), quality (standard, hd), style (natural, vivid), and n (always 1 for GPT-image-1.5).

Click to flip back

Question

What is GPT-image-1.5's prompt rewriting feature?

Click or press Enter to reveal answer

Answer

GPT-image-1.5 automatically expands and enhances your prompt with additional details to improve image quality. You can see the revised prompt in the API response via revised_prompt.

Click to flip back

Question

What safety features does GPT-image-1.5 include in Azure?

Click or press Enter to reveal answer

Answer

Blocks violent/sexual/harmful image requests, blocks real people by name, adds C2PA provenance metadata (AI-generated watermark), and applies configurable content filters.

Click to flip back

Knowledge Check

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?

Knowledge Check

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.