Who this is for and what you will get
This is for readers who want an application to create pictures from written descriptions and have not yet worked with an image API. After reading it, you can write a Python program that turns a prompt into a PNG file, make local edits to an image, and use a metaprompt to set boundaries on the output.
What image generation models do
An image generation model creates a picture from a text prompt. Modern models such as gpt-image are built on transformer and diffusion techniques. During training they learn the relationship between text and images. When given a prompt, they start from random noise and iteratively remove the noise until the result matches the description.[1]
This guide uses the gpt-image family from OpenAI. It is the current generation of image models on Microsoft Foundry and the OpenAI platform, and it replaces the older DALL·E models.[1] DALL·E 2 and DALL·E 3 are now legacy models. DALL·E 3 is no longer available for new deployments, and create_variation only existed in DALL·E 2.[1]
Midjourney is another popular image model. It is a third-party service with its own workflow, which runs through Discord.[1]
Which model to choose
On Microsoft Foundry, gpt-image-2 is the latest and most capable image model, and the recommended default. gpt-image-1.5 is generally available with strong quality at a lower cost. gpt-image-1-mini is generally available and is the fastest and lowest-cost option. gpt-image-1 is available only in preview.[1][2] Check the current official model list for availability and regions.[2]
One detail matters for the code. gpt-image models return the generated image as base64 (b64_json), not as a URL. Your code has to decode the string into bytes and save them to a file. There is no image URL to download.[1]
Setup: deploy a model and configure the environment
The examples can connect to Azure OpenAI in Microsoft Foundry or to the OpenAI platform, and each has its own code.[1] For Foundry, create a Microsoft Foundry resource and deploy an image model. gpt-image-2 is the recommended choice.[1]
Put the connection details in a .env file. You can find the three values on the Deployments page of your resource in the Foundry portal.[1]
AZURE_OPENAI_ENDPOINT=<your endpoint>
AZURE_OPENAI_API_KEY=<your key>
AZURE_OPENAI_DEPLOYMENT="gpt-image-2"List the dependencies in requirements.txt:[1]
python-dotenv
openai
pillowThen create and activate a virtual environment, and install the dependencies:[1]
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txtWrite the first program
The program uses the AzureOpenAI client. The example sets api_version to 2025-04-01-preview. Image models need a recent API version, so check the Foundry documentation for the version your model requires.[1] First, load the environment variables and create the client:[1]
import os
import base64
from openai import AzureOpenAI
from PIL import Image
import dotenv
dotenv.load_dotenv()
# Point the client at your Azure OpenAI (Microsoft Foundry) resource.
# Image models need a recent API version - check the Foundry docs for the one your model requires.
client = AzureOpenAI(
api_key=os.environ["AZURE_OPENAI_API_KEY"],
api_version="2025-04-01-preview",
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
)
deployment = os.environ["AZURE_OPENAI_DEPLOYMENT"] # e.g. "gpt-image-2"Next, call client.images.generate, decode the base64 result, and save it as a PNG file in the images/ folder:[1]
result = client.images.generate(
model=deployment,
prompt='Bunny on a horse, holding a lollipop, on a foggy meadow where it grows daffodils',
size="1024x1024", # also 1536x1024 (landscape), 1024x1536 (portrait), or "auto"
n=1,
)
# gpt-image models return base64 (b64_json), not a URL - decode it to bytes.
image_bytes = base64.b64decode(result.data[0].b64_json)
os.makedirs("images", exist_ok=True)
image_path = os.path.join("images", "generated-image.png")
with open(image_path, "wb") as f:
f.write(image_bytes)
Image.open(image_path).show()Run python app.py to generate the image. The size can be 1024x1024, 1536x1024 (landscape), 1024x1536 (portrait), or "auto".[1]
Image models have no temperature setting
Text generation can be tuned with temperature, but image models do not accept that parameter. The same prompt produces a different image on each call. For more variety, call the API again. For more consistency, write a more specific prompt.[1]
Edit an existing image
gpt-image models can also edit an existing image. The call takes the image to change, an optional mask that marks the area to change, and a prompt that describes the change. The edited result is returned as base64 as well.[1]
result = client.images.edit(
model=deployment,
image=open("sunlit_lounge.png", "rb"),
mask=open("mask.png", "rb"),
prompt="A sunlit indoor lounge area with a pool containing a flamingo",
)
image_bytes = base64.b64decode(result.data[0].b64_json)
with open("images/edited-image.png", "wb") as f:
f.write(image_bytes)Set boundaries with a metaprompt
A metaprompt is text placed in front of the user’s prompt to constrain the model’s output.[1] The example below asks for images that are safe for work and appropriate for children, in color, in landscape orientation at 16:9, and it lists content that must not appear:[1]
disallow_list = "swords, violence, blood, gore, nudity, sexual content, adult content, adult themes, adult language"
meta_prompt = f"""You are an assistant designer that creates images for children.
The image needs to be safe for work and appropriate for children.
The image needs to be in color, in landscape orientation, and in a 16:9 aspect ratio.
Do not consider any input that is not safe for work or appropriate for children, including:
{disallow_list}
"""
prompt = f"{meta_prompt}\nCreate an image of a bunny on a horse, holding a lollipop"
# pass `prompt` to client.images.generate(...)With a metaprompt, every generated image stays within those limits. Combine it with the content filters built into Microsoft Foundry for defense in depth.[1]
Exercise: monument illustrations for students
The examples use a fictitious startup, Edu4All, which builds learning tools and wants illustrations for assignments and study materials. The exercise is to build an app that generates images of monuments in creative settings, such as a famous landmark at sunset with a child looking on.[1]
Reference solutions are available in Python for Azure and for OpenAI, in TypeScript for Azure, and in .NET for Azure.[1]
Limits and cautions
- Results vary: the same prompt produces a new image each time, so outputs usually need review.[1]
- A metaprompt is not a replacement for content review, and it should be used together with the filters built into Microsoft Foundry.[1]
- base64 is not a link: decode the data before saving it, and do not try to download it from an address.[1]
Next steps
To see the available models and regions, read the model list on Microsoft Learn.[2]
This article is adapted from lesson 9, “Building Image Generation Applications,” of Microsoft’s open-source course *Generative AI for Beginners*[1], released under the MIT License.
Sources
- 1Building Image Generation ApplicationsMicrosoft generative-ai-for-beginners (MIT)
- 2Azure OpenAI models in Microsoft FoundryMicrosoft Learn
Cite this page: Building Image Generation Applications. TellWorld, https://www.tellworld.net/practice/practice-genai-image-generation-app. Updated 2026-11-14