Examples · Programming
Pictures in Python
Generate a picture and save the PNG, ask for two variants at once with the n parameter, and use a seed to get the same picture again.
Intermediate · 30 min
What you need
A key and the openai library, as in Your first API call in Python.
The model
Z-Image Turbo (z-image-turbo), the school's picture model. It makes PNG pictures in three sizes: 1024x1024 (square, the default), 1024x1536 (portrait) and 1536x1024 (landscape). A picture takes about ten seconds, a little more in portrait or landscape.
The program
Save it as picture.py:
import base64
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.ai.itsincom.org/v1",
api_key=os.environ["API_KEY"],
)
result = client.images.generate(
model="z-image-turbo",
prompt="A small robot watering plants in a greenhouse, flat vector illustration, soft daylight",
size="1024x1024",
)
with open("robot.png", "wb") as f:
f.write(base64.b64decode(result.data[0].b64_json))
print("robot.png is ready")
The picture comes back inside the answer, as text in base64: b64decode turns it back into the bytes of a PNG.
Two variants with n
Save it as variants.py:
import base64
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.ai.itsincom.org/v1",
api_key=os.environ["API_KEY"],
)
result = client.images.generate(
model="z-image-turbo",
prompt="A small robot watering plants in a greenhouse, flat vector illustration, soft daylight",
n=2,
)
for i, picture in enumerate(result.data, 1):
with open(f"robot-{i}.png", "wb") as f:
f.write(base64.b64decode(picture.b64_json))
print(len(result.data), "pictures")
n goes from 1 to 4. The pictures are made one after the other, so two take twice as long.
The same picture again, with seed
Every request draws a new picture, even with the same prompt. A seed fixes the starting point: the same prompt with the same seed gives the same picture again. The library has no seed argument, so it goes in extra_body. Save it as same_again.py:
import base64
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.ai.itsincom.org/v1",
api_key=os.environ["API_KEY"],
)
pictures = []
for attempt in (1, 2):
result = client.images.generate(
model="z-image-turbo",
prompt="A small robot watering plants in a greenhouse, flat vector illustration, soft daylight",
extra_body={"seed": 1234},
)
pictures.append(base64.b64decode(result.data[0].b64_json))
with open(f"seed-{attempt}.png", "wb") as f:
f.write(pictures[-1])
print("the same picture" if pictures[0] == pictures[1] else "two different pictures")
What you should see
picture.pyprintsrobot.png is ready: a small robot with a watering can, among young plants.variants.pyprints2 pictures: the same idea drawn twice, with the robot and the plants in different places.same_again.pyprintsthe same picture. When we ran it, the two files were identical, byte for byte.
Good to know
- A seed is not forever. The same prompt and seed give the same picture as long as the engine stays the same; a new version of it can draw something else. When you like a picture, keep the PNG, not only its seed.
- Some pictures are refused. Real people, nudity, blood, hate, famous characters and brands are not drawn; fantasy and game scenes with stylised action are fine. A refused prompt raises
openai.BadRequestError, withe.codeset toprompt_refusedand the reason in the message. - Every picture is checked after it is made, too. A blocked picture is not delivered and costs nothing. With
nabove 1 you get the pictures that passed, which can be fewer than you asked: that is whyvariants.pycounts them. If none passed,e.codeisoutput_refused. - Only six fields:
model,prompt,n,size,response_formatandseed. OpenAI'squality,styleorbackgroundare refused with a400that names them: there are no transparent pictures here. - A link instead of the picture: with
response_format="url",result.data[0].urlis a link that works without the key for 24 hours, handy for a web page. Then the file is deleted. - A picture costs 0.01 credits, only if it is delivered.
- Marked as AI: the PNG says in its metadata that it was made by artificial intelligence. When you publish it, say so in words too.
- How to write a good prompt, and how to get words written right inside a picture: A picture from a description.
Exercise
- Keep the seed and change one word of the prompt: “greenhouse” becomes “space station”. What stays the same?
- Make the same prompt in the three sizes. What does the model put in the extra space?
- Ask for
n=4, save the pictures in a folder, and write a small HTML page that shows them side by side. - Write a function
picture(prompt, path, seed=None)and make one picture for every line of a text file. - Ask for
response_format="url", print the link and open it in your browser.
