AlgoMaster Logo

Exercise: Text-to-Image Generation

3 min readUpdated June 22, 2026
Listen to this chapter
Unlock Audio

Exercise 1: Generate an Image and a Variation

Generate an image from a text prompt, then generate a second version with one detail changed, so you can see how prompt edits steer the output.

Image generation is prompt engineering for pixels. The fastest way to learn what the model responds to is to generate one image, change a single attribute in the prompt (style, color, setting), and generate again. Holding everything else fixed shows you which words actually move the result.

Requirements

  • Generate an image from a base prompt with client.images.generate.
  • Change one attribute in the prompt and generate a second image.
  • Print confirmation and the size of each returned image payload (you cannot display the image inline here).
main.py
Loading...

Expected Output

Both calls return image data, and the only intended difference between them is the changed attribute.

Solution

main.py
Loading...

The two calls are identical except for one word in the prompt, which isolates that attribute as the cause of any difference, exactly the controlled-change approach you used with temperature earlier. The model returns the image as base64, which you decode into bytes (and would normally write to a file or display). Building intuition this way, one attribute at a time, is how you learn to write image prompts that produce what you intend rather than something close to it.

Exercise 2: Generate the Same Subject in Several Styles

Holding the subject fixed while varying the style isolates style as a prompt dimension. Generate the same scene in several styles and confirm each renders. This is how you build a style picker or A/B different looks for the same content.

Requirements

  • Keep one subject and a list of styles.
  • Generate an image for each subject, style combination.
  • Print the style and the byte size of each generated image.
main.py
Loading...

Expected Output

Three images are generated, one per style, each returning image data.

Solution

main.py
Loading...

Appending the style to a fixed subject keeps everything else constant, so the only variable across the three images is the rendering style, which is what lets you compare them fairly. Looping over a list of styles is the same approach you would use to offer a user several looks or to test which style performs best for a use case. Each call returns independent image data, decoded here just to confirm it arrived.