Python Generation

Generating and Editing an Image from Python

The Python SDK (pip 21,050 install openai, 3.19.2 here) mirrors the Node.js 2,131 one; an open file works as the image.

giftcard.py: a gift card, then its winter editionPython
# giftcard.py: generate, then edit, with the Python SDK (pip install openai)
import base64
from openai import OpenAI
client = OpenAI()                                      # reads OPENAI_API_KEY
card = client.images.generate(
    model='gpt-image-2.5-flare', size='1536x1024', quality='low',
    prompt='A BookNest gift card: a small bird on an open book beside a stack of six books, '
           'flat vector, deep blue, saffron and cream, the words "BookNest Gift Card"')
open('giftcard.png', 'wb').write(base64.b64decode(card.data[0].b64_json))
with open('giftcard.png', 'rb') as image:
    winter = client.images.edit(
        model='gpt-image-2.5-sunburst', image=image, size='1536x1024', quality='low',
        prompt='Make this a winter edition: light snow and a knitted scarf on the bird. '
               'Keep the layout, colors and text unchanged.')
open('giftcard-winter.png', 'wb').write(base64.b64decode(winter.data[0].b64_json))
for result in card, winter:
    print(result.size, result.usage.input_tokens_details, result.usage.output_tokens)
Output
1536x1024 UsageInputTokensDetails(image_tokens=0, text_tokens=45) 158
1536x1024 UsageInputTokensDetails(image_tokens=1536, text_tokens=41) 158
bookmark.mjs's 512x1536 WebP ($0.0019), giftcard.py's card (Flare, $0.0050) and its winter edit (Sunburst, $0.0172), all low quality
bookmark.mjs's 512x1536 WebP ($0.0019), giftcard.py's card (Flare, $0.0050) and its winter edit (Sunburst, $0.0172), all low quality

The edit kept the lettering, layout and bird and added snow and a scarf; its 1536x1024 input cost 1,536 tokens.

generate, then edit: a BookNest gift card and its winter edition, where the edit keeps layout and letteringHTMLLive
<!doctype html>
<style>
  body { margin: 0; padding: 8px; background: #fafaf7; font: 12px system-ui, sans-serif; color: #263238; }
  canvas { display: block; max-width: 100%; }
</style>
<p><label><input type="checkbox" id="snow" checked> images.edit: "Make this a winter edition: light snow and a knitted scarf on the bird. Keep the layout, colors and text unchanged."</label></p>
<canvas id="c" width="600" height="230"></canvas>
<script>
  const ctx = document.getElementById('c').getContext('2d');
  function card(x, y, winter) {
    const W = 285, H = 190;
    ctx.save();  ctx.translate(x, y);
    ctx.fillStyle = '#fff3e0';  ctx.beginPath();  ctx.roundRect(0, 0, W, H, 12);  ctx.fill();
    ctx.fillStyle = '#1f5f8b';  ctx.fillRect(0, 130, W, 60);
    ['#1f5f8b', '#5b3f99', '#e09a10', '#3f7d3a', '#b5452f', '#2a9d8f'].forEach((c, i) => {
      ctx.fillStyle = c;  ctx.fillRect(180 + (i % 2) * 6, 118 - i * 14, 80, 12);
    });
    ctx.fillStyle = '#e09a10';                                          // open book
    ctx.beginPath();  ctx.moveTo(40, 120);  ctx.quadraticCurveTo(80, 100, 120, 120);  ctx.lineTo(120, 128);  ctx.quadraticCurveTo(80, 110, 40, 128);  ctx.fill();
    ctx.fillStyle = '#1f5f8b';  ctx.beginPath();  ctx.ellipse(82, 96, 18, 13, 0, 0, 7);  ctx.fill();   // bird
    ctx.beginPath();  ctx.arc(96, 82, 9, 0, 7);  ctx.fill();
    ctx.fillStyle = '#e09a10';  ctx.beginPath();  ctx.moveTo(104, 82);  ctx.lineTo(112, 85);  ctx.lineTo(104, 88);  ctx.fill();
    ctx.fillStyle = '#fff3e0';  ctx.font = 'bold 20px Georgia';  ctx.fillText('BookNest Gift Card', 20, 168);
    if (winter) {
      ctx.fillStyle = '#b5452f';  ctx.fillRect(86, 88, 20, 6);  ctx.fillRect(98, 92, 5, 14);       // knitted scarf
      ctx.fillStyle = '#ffffff';
      for (let i = 0; i < 70; i++) { ctx.beginPath();  ctx.arc((i * 53) % W, (i * 29) % 130, 1.5 + (i % 3), 0, 7);  ctx.fill(); }
    }
    ctx.restore();
  }
  function draw() {
    ctx.clearRect(0, 0, 600, 230);
    card(5, 5, false);  card(305, 5, document.getElementById('snow').checked);
    ctx.fillStyle = '#263238';  ctx.font = '11px system-ui';
    ctx.fillText('images.generate · Flare · 1536x1024 · $0.0050', 5, 214);
    ctx.fillText('images.edit · Sunburst · 1,536 image tokens in · $0.0172', 305, 214);
  }
  document.getElementById('snow').onchange = draw;
  draw();
</script>