Your profile

One profile for every MSDEVBUILD site. Only you can see it, and the site owner if you write in.

Lesson 25 of 39
❮Prev Next❯

Generate and Edit Images

Module 3: Computer Vision ⏱ 9 min read Updated

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    30-second answer

    To generate an image, you send a prompt to a gpt-image deployment and choose the size, quality, number of images and output format. To edit an image, you send the picture, a prompt and, for a precise change, a mask: a PNG of the same size where the transparent area is the only part the model may change. The image comes back as base64, and a transparent background needs PNG.

    Simulate it » No Azure account needed

    Exam objective: 3.1.1 Implement a solution that generates images from text prompts and reference media. 3.1.3 Configure image-editing workflows, including inpainting, mask-based edits and prompt-driven modifications. 3.1.5 Select and apply appropriate generation and editing controls provided by the platform. (Skills measured as of April 16, 2026)

    Simulations

    Feel it before you read it. No account needed, nothing leaves this page.

    Azure portal: Generate an image and edit it with a mask Deploy gpt-image-1, generate an image and edit it with a mask.
    </> Embed

    Embed this simulation

    Free for trainers, teachers and bloggers. Paste this into your page:

    Loading the portal…

    Open <strong>Build → Models + endpoints</strong> and deploy <strong>gpt-image-1</strong>. The deployment name is what your code sends as the model.

    👆 Click the glowing item in the portal to continue

    Read the steps as text
    1. Open Build → Models + endpoints and deploy gpt-image-1. The deployment name is what your code sends as the model.
    2. In the Images playground write the prompt, then set the size, quality, number of images and output format. Subject, style, light and layout belong in the prompt.
    3. In code, gpt-image models return the picture as base64. There's no URL to download, so decode and save it.
    4. To change only the sky, upload the image and a mask. The mask is a PNG of the same size, and the transparent area is what the model may repaint.
    5. Keep the controls straight: size, quality, n, format, background and the mask. A guardrail on the deployment still checks every prompt.

    🧪 Simulation for learning. Screens are simplified versions of the Azure portal; names, prices and IDs are examples. Nothing here touches real Azure.

    😟 THE PROBLEM

    The design team needs fifty product backgrounds by Friday, and every edit redraws the whole picture.

    😊 THE SOLUTION

    Generate with a gpt-image deployment and use a mask so only the background changes.

    • Blank page for every banner✓ Prompt with size, quality and format
    • An edit changes the logo too✓ A mask: transparent where change is allowed
    • A cut-out needs a clear background✓ PNG with background set to transparent
    🛠️ Solved with: gpt-image in Foundry

    Start with a question

    Priya Sweets has one good photo of the shop, but the sky is flat grey. Can the owner fix just the sky without hiring a designer?

    Yes, and that’s a different job from asking for a brand new picture.

    When you ask a model to generate, it starts from a blank canvas and paints what your words describe. When you ask it to edit, it starts from your photo and has to decide what to keep. A mask is how you tell it: change this part, leave the rest.

    Explain it like I’m 10

    The technical explanation

    In Microsoft Foundry you deploy an image model such as gpt-image-1. Your code calls the images API with the deployment name in the model field.

    Generate

    import base64, os
    from openai import OpenAI
    from azure.identity import DefaultAzureCredential, get_bearer_token_provider
    
    token = get_bearer_token_provider(DefaultAzureCredential(), "https://ai.azure.com/.default")
    client = OpenAI(base_url=os.environ["AZURE_OPENAI_ENDPOINT"].rstrip("/") + "/openai/v1/", api_key=token)
    
    result = client.images.generate(
        model="gpt-image-1",          # your deployment name
        prompt="Festival banner for Priya Sweets, laddoos on a brass tray, warm light",
        size="1536x1024", quality="low", n=2,
        output_format="png", background="auto",
    )
    for i, item in enumerate(result.data, 1):
        open(f"banner_{i}.png", "wb").write(base64.b64decode(item.b64_json))   # base64 only

    The controls you’re expected to know (checked on Microsoft Learn, 25 November 2026):

    SettingValuesNotes
    size1024x1024, 1024x1536, 1536x1024Square, portrait, landscape
    qualitylow, medium, highHigher costs more and takes longer
    n1 to 10Images per request
    output_formatpng, jpegNo WEBP for output
    backgroundauto, transparentTransparent needs PNG

    Edit with a mask

    result = client.images.edit(
        model="gpt-image-1",
        image=open("shop-front.png", "rb"),
        mask=open("mask.png", "rb"),             # PNG, same size, transparent = may change
        prompt="Replace the grey sky with a warm sunset. Keep everything else unchanged.",
        input_fidelity="high",                   # keep faces, logos and text close to the original
    )

    An edit works in three ways. Mask-based (inpainting) changes only the transparent area. Prompt-driven edits send the image with no mask, and the model decides what to touch, which is fine for a style change but risky for exact details. You can also send reference images so a new picture borrows a product or a style.

    What guardrails do here

    A guardrail on the deployment checks the prompt, and unsafe requests are refused before any image is made. Generated images also carry content credentials, so people can see they came from AI. Both belong in your design, not in an afterthought.

    Architect level Why does the same prompt give a different picture every time?

    Image models sample, so a prompt is a brief, not a recipe. Put the subject, style, light and layout in the prompt, then use n to get several options. When one detail must be exact, such as a logo, don’t ask the model to draw it from memory. Supply it as a reference image or add it afterwards in your own code, and review the result.

    Real-world business scenario

    Food retail

    Priya Sweets

    The problem
    The marketing team made fifty festival banners by hand each year, and every photo edit risked changing the shop sign.
    What they used
    A gpt-image deployment in Foundry. Banners are generated at low quality for drafts, photos are edited with a mask, and the chosen draft is rendered again at high quality.
    Why that choice
    The mask keeps the sign and counters untouched, PNG gives clean logos, and the deployment's guardrail checks every prompt.
    The result
    The team reviewed ten drafts in the time it used to take to brief one, and no sign was altered by mistake.

    When to use it and when not to

    ✓ Use it when…

    • You need new marketing or concept images from a text brief.
    • You must change one region of an existing image and keep the rest.
    • You want several variations in a single request.

    ✕ Think twice when…

    • You need an exact logo or legal text drawn perfectly. Place the real asset yourself.
    • You need to read or classify photos you already have.
    • The image could show a real person without their consent.

    ↔ Alternatives to consider

    • A multimodal chat model to describe or question existing images.
    • Content Understanding when every image needs the same fields.
    Ready for more?The full lesson adds the technical explanation, when to use it, interview questions and revision notes.

    Test yourself with exercises

    5 original practice questions in the AI-103 style. Pick your answers, then check them. You will see why each option is right or wrong. Score 75% or more and this topic is marked complete.

    Question 1

    Question 1 of 5

    A marketing application uses a gpt-image deployment to replace the sky in an existing product photo. All other parts of the photo must remain unchanged. What should the application send with the edit request?

    Question 2

    Question 2 of 5

    A web application must generate a company logo that has a transparent background by using a gpt-image deployment. Which output format must the request specify?

    Question 3

    Question 3 of 5

    An application calls the images generation API of a gpt-image-1 deployment and tries to read a download URL from the response. The property is empty. What is the cause?

    Question 4

    Question 4 of 5

    A solution must create five alternative banner images from one prompt in a single request at the lowest cost per request. Which settings should the developer use?

    Question 5

    Question 5 of 5

    A developer edits a photo of a staff member and notices that the face and the logo on the shirt change slightly after every edit. The developer must keep these details as close to the original as possible. Which setting should the developer change?

    Interview preparation

    Say your answer out loud first, then open the model answer. Interviewers care more about your reasoning than the exact words.

    Beginner How do you change only the sky in a photo with a gpt-image model?

    Model answer

    I call the image edit operation with the photo, a prompt that describes the new sky and a mask. The mask is a PNG the same size as the photo, and the sky area is transparent. Only that area can change.

    Interviewer follows up

    What if I leave the mask out?

    You

    Then the prompt is the only guide, and the model can redraw other parts too. I'd add the mask and set input fidelity to high.

    Intermediate A designer wants a logo with no background. What do you set?

    Model answer

    I set the output format to PNG and the background to transparent. JPEG can't hold transparency, and WEBP isn't supported for output here.

    Experienced developer How would you control cost for a banner tool that makes many variations?

    Model answer

    I'd use low quality for drafts and n up to 10 per request, and only render the chosen draft at high quality. I'd also set a token budget on the project and a guardrail on the deployment.

    Scenario The app tries to read a URL from the response and gets nothing. Why?

    Model answer

    gpt-image models return base64 data only. I'd decode the b64_json value, save it as a file or upload it to Blob Storage, and share that link.

    Quick revision

    Key points

    • Generation and edits use a gpt-image deployment, and your code sends the deployment name as the model.
    • Sizes are 1024x1024, 1024x1536 and 1536x1024. Quality is low, medium or high. n is 1 to 10.
    • Output format is PNG or JPEG. A transparent background needs PNG.
    • A mask is a PNG of the same size, and transparent pixels are the area that can change.
    • Images come back as base64. Guardrails check prompts, and generated images carry content credentials.

    Important terminology

    gpt-image
    The family of Azure OpenAI image models, such as gpt-image-1, that generate and edit images from text prompts and reference images.
    Mask
    A PNG with the same dimensions as the image where transparent pixels mark the area an edit may change.
    Inpainting
    Repainting only a selected region of an existing image, usually with a mask and a prompt.
    Input fidelity
    An edit setting (low or high) that controls how closely the model keeps faces, logos and fine details from the input image.
    Content credentials
    Provenance metadata added to generated images so that others can tell they were made by AI.

    Common mistakes

    • Editing without a mask and expecting only one area to change.
    • Using a mask with different dimensions from the image.
    • Asking for WEBP output or a transparent background in JPEG.

    Exam tips

    • "Change only part of an image" = mask-based edit (transparent area).
    • "Transparent background" = PNG output with background transparent.
    • "Keep faces and logos close to the original" = input_fidelity high.

    Five quick revision questions

    1. 1.What does the transparent area of a mask mean?Show

      It is the area the model may change.

    2. 2.Which format gives a transparent background?Show

      PNG.

    3. 3.How many images can one request create?Show

      1 to 10, set with n.

    4. 4.In what form does gpt-image return the picture?Show

      Base64 data, not a URL.

    5. 5.Which setting keeps faces and logos closer to the input?Show

      input_fidelity set to high.

    Official references

    Finished this lesson? Tick it off. Your progress is saved in this browser.

    Next up: Generate and Edit Video

    Studying with a friend? Send them this lesson

    It's free and needs no sign-up. Their progress saves in their own browser.

    Lesson done? Make it stick with a quick gameSort It, Match the Pairs, Lightning Round and Who Am I? Earn XP and badges. Play ❯

    Install MSDEVBUILD Azure

    Free · no account · works offline

    Save the whole course for offline ›

    Hi, I’m your Nimbus

    Your study buddy. Pick what we do next.

    MSDEVBUILD · Multi-Stack Dev Build

    Sign in to MSDEVBUILD

    One account for Azure certifications and the blog: sign in here and you are signed in on both. Your XP, streak, practice deck and certificate stay in sync on every device. Optional: everything works without an account.