VITON13School

Generative AI for BeginnersLesson 10 of 21

Building image generation apps

Text models write; image models draw from a description. The API call is short, but good results depend on the prompt, the settings and the rules you put around it. You will give Study Buddy the power to illustrate its own flashcards.

Course by , Founder of VITON13 SCHOOL
  • Part 2: Build
  • 40 min

You will be able to

  • Explain in simple terms how a text-to-image model turns a prompt into a picture
  • Write image prompts that control subject, style, composition and what to leave out
  • Generate an image through an OpenAI-compatible endpoint and save it as a file
  • Describe how image editing with a mask works
  • Add safety rules and check the rights before you publish generated images

How image models work, briefly

Most current image generators learn the link between pictures and their captions from very large collections of both. Many of them use diffusion: the model starts from random noise and removes it step by step, guided by your text, until an image that fits the description appears. Others predict the image piece by piece, much as a language model predicts tokens.

You do not need the maths to build with them, but two consequences matter. First, the same prompt gives a different image each time, because the starting noise changes. Second, the model has no real understanding of counts, spelling or physics, so it can draw six fingers, misspelled signs or impossible shadows. Look at every picture with your own eyes before a learner sees it, especially diagrams that are meant to teach something precise.

Image models are separate from chat models. Some providers offer both behind one key, some offer only text, and Anthropic's Claude, for example, reads images but does not generate them. That is why the example uses its own LLM_IMAGE_MODEL variable, and you may need a different LLM_BASE_URL for this lesson.

Writing prompts for pictures

An image prompt is closer to a brief for an illustrator than to a question. Name the subject first, then add the details that change the result most. Short, concrete phrases work better than long poetic sentences.

Keep a fixed style block and change only the subject. For Study Buddy, every flashcard can share the same style line, which gives the set a consistent look and makes it easier to see which prompt change caused which difference.

Avoid asking for text inside the image unless your model handles lettering well. Even good models still make spelling mistakes, so put the words on the card in your own interface and keep the picture wordless.

  • Subject: what is in the picture, for example a plant cell cut in half
  • Style: flat vector, watercolour, pencil sketch, photo, 3D render
  • Composition: close-up or wide, centred, plain background, viewpoint
  • Light and colour: soft daylight, high contrast, a limited palette
  • Exclusions: no text, no people, no logos, stated plainly

Sizes, formats and other settings

The size parameter sets the shape and resolution. A square such as 1024x1024 is supported almost everywhere, while the exact landscape and portrait sizes differ between models, so check your provider's documentation before you hard-code one. Larger images take longer and usually cost more.

Providers return the result in one of two ways. Some send the picture itself as base64 text in b64_json, which you decode to bytes and save. Others send a url that points to the file for a limited time, so download it at once rather than storing the link.

Image endpoints do not take a temperature setting. To get variety, call again with the same prompt; to get consistency, make the prompt more specific. Some models also accept a quality or background option, but these names are not standard, so treat them as provider extras.

Save every prompt next to the file it produced. When a picture is good you will want to make more like it, and when one is bad you will want to know why.

Image calls are slower than text calls, often several seconds or more. In an app, show a clear progress state, generate in the background where you can, and cache finished pictures so the same flashcard is never drawn twice.

Editing an existing image

Many image models can also edit. With client.images.edit you send the original picture, a prompt describing the change and, optionally, a mask: a second image of the same size where the transparent area marks what may be repainted. Everything outside the mask stays as it was.

Masks are useful for small, controlled changes, such as replacing the background of a diagram or adding a missing label area. Without a mask the model may redraw more than you intended, so compare the edited image with the original before you keep it.

Edits are a separate capability, so confirm that your chosen model supports them. Some accept several input images to combine, some need a specific file format such as PNG, and some do not edit at all.

Safety and rights

Put boundaries in code, not only in the user's prompt. A fixed prefix or suffix that states the audience and lists what is not allowed, such as violence or real people, travels with every request. Most providers also run their own content filters, and a refused request comes back as an error you should catch and explain kindly.

Be careful with real people, brands and living artists. Pictures of identifiable people can mislead or hurt them, logos can break trademark rules, and prompts that copy a living artist's style raise fair questions about consent even where the law is unclear.

Ownership of generated images is not settled in the same way everywhere. Read your provider's terms and the rules where you publish before you sell or license generated pictures, and label images as AI-generated when people could mistake them for photos or for an artist's own work.

Accessibility applies to generated pictures too. Every image in Study Buddy needs a short text alternative that says what it shows, and the subject line you wrote for the prompt is a good starting point for it.

Finally, keep a person in the loop for anything public. A quick human look before a picture goes on a course page catches the odd extra limb, the accidental logo and the image that is technically safe but simply wrong for the lesson.

Example

Draw a flashcard picture and save it

import base64import osimport urllib.requestfrom openai import OpenAI client = OpenAI(base_url=os.environ["LLM_BASE_URL"], api_key=os.environ["LLM_API_KEY"])IMAGE_MODEL = os.environ["LLM_IMAGE_MODEL"]  # an image model offered by the same endpoint STYLE = ("Flat vector illustration for a study flashcard, plain white background, "         "clear shapes, no text or letters, no real people, no brand logos.") def draw(subject, path):    result = client.images.generate(        model=IMAGE_MODEL,        prompt=f"{subject}. {STYLE}",        size="1024x1024",    )    image = result.data[0]    if image.b64_json:  # many current models return base64 data        data = base64.b64decode(image.b64_json)    else:  # others return a short-lived link to the file        data = urllib.request.urlopen(image.url).read()    with open(path, "wb") as f:        f.write(data)    return path print(draw("A plant cell with the chloroplasts shown in bright green", "cell.png"))

Set LLM_IMAGE_MODEL to an image model that your endpoint offers. The code handles both answer styles, base64 data and a temporary link, so it works across providers that follow the OpenAI image API.

Practice

Illustrate three Study Buddy flashcards

  1. Pick three concepts from your notes.txt and write one short subject line for each.
  2. Run the example once per subject and save the files as card-1.png, card-2.png and card-3.png in an images folder.
  3. Save the full prompt for each picture in images/prompts.txt, one line per file.
  4. Change only the STYLE line, for example from flat vector to pencil sketch, and generate the first card again.
  5. Open all four images side by side and mark which ones contain stray letters, people or logos despite the rules.

Done when: You have four saved images with their prompts written down, the first three share one recognisable style, and you can point to which part of the prompt changed the fourth.

Common mistakes

  • Storing a returned image link instead of downloading the file, then finding the link has expired.
  • Rewriting the whole prompt between attempts, so you cannot tell which change made the picture better or worse.
  • Asking the model to write the card's text inside the image and publishing the misspellings.
  • Publishing generated pictures of real people or brands without checking consent, terms and local rules.

Check yourself

Pick an answer to see whether it is right and why.

You run the same image prompt twice and get two different pictures. Why?

Right.Diffusion-style models begin from a fresh random starting point on every call, which is also why image endpoints have no temperature setting.

Not quite.The right answer is “Generation starts from random noise, so each run takes a different path”. Diffusion-style models begin from a fresh random starting point on every call, which is also why image endpoints have no temperature setting.

What does a mask do when you edit an image?

Right.The transparent part of the mask tells the model where change is allowed, so everything else stays the same.

Not quite.The right answer is “It marks the area the model may repaint and protects the rest”. The transparent part of the mask tells the model where change is allowed, so everything else stays the same.

Which is the best way to keep children's flashcards safe?

Right.Rules in code apply to every request no matter what the user types, and provider filters add a second layer you should handle gracefully.

Not quite.The right answer is “A fixed safety text in your code added to every prompt, plus handling the provider's refusals”. Rules in code apply to every request no matter what the user types, and provider filters add a second layer you should handle gracefully.

From the founder

Change one thing at a time in your image prompts and write down what each change did. After three rounds you will have your own small style guide, which is worth more than any list of magic words.
Vitalii Tarasov, Founder of VITON13 SCHOOL

Course by Vitalii Tarasov, VITON13 SCHOOL. Draws in part on Microsoft's Generative AI for Beginners, MIT licence, Copyright (c) Microsoft Corporation. microsoft/generative-ai-for-beginners