The complete guide to AI image generation

By

Updated

Tagged: AI

Learn AI image generation from prompts to finished assets, with hosted models, creative tools, local workflows, ComfyUI, and JavaScript APIs.

You can generate an image by describing it in ChatGPT. You can also download a model, run it on your computer, and build a workflow that produces hundreds of assets with the same settings. In between there are design apps, image editors, hosted APIs and visual workflow builders, each giving you a different kind of control.

In this guide we’ll work on one small project: an illustration for a fictional bicycle repair shop called Officina. We’ll create it, change it, prepare it for a website, and then do the same through code and a local model. I haven’t benchmarked these models, so use the exercises to test them on your own work.

Start with the job you need to do

Exploring an idea, producing an illustration, editing a photo and making variations need different things. For a background illustration you can accept many compositions. For a product photo, changing the product’s shape makes the result useless.

So write down what you need before opening a tool. For Officina, the brief is:

A wide illustration for the homepage of a bicycle repair shop. One blue city bicycle on the right, a small repair stand behind it, and a plain light background. Leave the left third empty for a heading. Use a flat editorial illustration style with dark outlines and a limited palette. The image contains no lettering. The final web asset will be 1600 × 900 pixels.

Now we can check each result: does the bicycle make sense, and is there room for the heading? The 1600 × 900 size is the deliverable, and we’ll resize or crop to it afterward. The headline stays in HTML, so it’s accessible, translatable and editable without generating a new picture.

Your jobCapability to look forA useful first route
Explore an ideaText-to-image and variationsA chat app or creative image tool
Change an existing pictureImage input and instruction-based editingA model with an explicit editing workflow
Fix a small regionMasks and inpaintingAn image editor or a local workflow
Keep a product recognizableReference inputs and careful preservationA reference-based editor, checked against the original
Match a sketch or poseStructural conditioningComfyUI with a compatible control model
Produce editable iconsVector outputA vector generator or SVG drawn in code
Repeat a process in an appAPI access and predictable output handlingA direct API or hosted inference provider
Keep inference on your machineDownloadable weights and a compatible runnerDraw Things, MFLUX, or ComfyUI

Model, product, and provider mean different things

A model is the trained system that generates or edits the image. GPT Image, FLUX and Qwen Image are model families.

A product is the interface around a model, like a canvas, a chat or layer editing. ChatGPT and ComfyUI are both products.

A provider runs a model for you, its own or someone else’s.

That’s why the same model can feel different in two apps: one rewrites your prompt and picks the settings, another exposes them. And installing a local interface doesn’t mean you generate locally, because a ComfyUI workflow can call a paid remote model through an API node.

From an image request to a fileA product accepts a prompt and references, then uses either a hosted model or a downloaded local model before saving the output.Prompt and referenceimagesProduct or workflowHosted model throughan APIDownloaded model onyour computerGenerated imageReview and preparethe asset

When you compare tools, ask which model runs, where it runs, what inputs it gets, and what you can save afterward.

How image generation works

You don’t need the math, but a bit of background explains why some controls matter.

During training, a model learns patterns from a dataset of images, captions, editing examples and other data. During inference, you use the trained model to produce an output from your prompt and references. Generating an image doesn’t train the model on your project. Whether a service keeps your uploads or trains on them is a separate policy you need to check.

A diffusion model learns to reverse a process that adds noise to data. To generate, it starts from noise and repeatedly updates it toward an image. Many systems do this in a compressed latent space and use a decoder to turn the result into pixels, as the latent diffusion paper explains. A latent isn’t a tiny photo that gets enlarged, it’s a set of learned features. The training resolution and the latent representation decide which sizes a model generates well, which is why an arbitrary width and height can give you poor results or an error.

A simplified latent image generation pipelineText becomes conditioning information, which guides repeated updates to noisy latents. A decoder turns the final latent representation into pixels.Text promptText encoderRepeated updatesguided by the promptNoise from a randomseedFinal latentrepresentationImage decoderPixels saved as animage

That’s one common pipeline, not every model. Many newer systems use flow matching (paper), which learns how to move from a noise distribution to the distribution of real data. You still see the same controls (prompt, seed, sampling schedule, steps), with a different training objective underneath.

A transformer is a neural network architecture, and “diffusion” and “transformer” can describe different parts of the same model. Multimodal means a system takes more than one kind of input or output, like a chat with several reference images that returns an edited image. Neither word tells you how the generation works internally, so judge the documented capabilities and the results.

Why objects and lettering still go wrong

The model draws something plausible. It doesn’t keep track of how objects work.

A bicycle has two wheels, a frame joining specific points, pedals, a chain and a handlebar connected to the steering. A generated one can look right at a glance and get several connections wrong. Text has the same problem: correct spelling needs the right marks in the right order.

So count objects, follow connections and read every word. For prices, instructions and technical diagrams, use editable text or draw the structure yourself.

The hosted models and products

This isn’t a leaderboard. Access changes often, and a subscription can expose different features from the developer API.

OpenAI: ChatGPT and GPT Image

ChatGPT is the conversational route: ask for an image, upload a reference, describe changes. It’s the easiest place to try the Officina brief without code.

GPT Image is the model family on OpenAI’s developer platform. The current image generation guide describes Sunburst for editing precision and Flare for everyday generation, and you’ll see the API model IDs in the code later. The Image API has generation and editing endpoints, while the Responses API can use image generation as a tool inside a conversation or agent workflow, where the conversation model and the image model can have different names.

The API exposes size, quality and format, and supported GPT Image models can output transparent images. Inspect the edges of a transparent cutout. A ChatGPT subscription and API billing are separate.

For Officina, generate an illustration, then change only the bicycle’s color and compare. A small edit can move nearby geometry or background details.

Google: Gemini and Nano Banana

Google calls Gemini’s native image generation Nano Banana. The official guide lists the models and their API names: Nano Banana 2.1, a Lite option and Nano Banana Pro, tuned differently for speed, complex tasks and editing. The cheapest one might not support the same reference workflow.

Gemini takes text and reference images and supports conversational changes, with aspect ratios and resolutions that depend on the model. Google says generated images include a SynthID watermark.

Older tutorials mention Imagen. Google’s Gemini API Imagen page says those models have shut down in that API and points to Nano Banana. Other Google services have their own docs.

For our illustration, test whether edits keep the composition across several turns, and save the first good image so you can go back to it.

Black Forest Labs: FLUX

FLUX includes hosted and downloadable models, with different licenses. The FLUX Image documentation covers generation, instruction-based editing, multiple references, and bounding boxes for composition. Bounding boxes are exactly what we need to keep the bicycle on the right and the left side empty.

The smaller FLUX Image Klein model has downloadable weights. Its docs mention prompt upsampling, where another model expands your short request into the detailed caption the generator expects. That stage can change your prompt and needs more memory, so use the full workflow supplied for your runner instead of downloading only the main weights.

Hosted FLUX 3 Image searches the web by default. Set grounding to false when the picture should only use your prompt and references.

A useful way to compare hosted systems

Use one brief, one set of references and written acceptance criteria. For Officina:

  1. Generate the initial illustration.
  2. Change the bicycle from blue to green.
  3. Replace the repair stand with a small toolbox.
  4. Extend the canvas to create more room on the left.
  5. Export the image and inspect it at its intended display size.

Note whether each step worked and how many attempts it took. A beautiful first image tells you little about editing, and one good sign in English tells you nothing about text in your language or layout.

Create the first Officina illustration

Open an image-capable chat app or creative tool, pick a landscape aspect ratio, paste the brief and generate a few alternatives.

Check them against the brief. Is the bicycle on the right, and the left side quiet? Are the wheels round and the frame sensible? Any accidental lettering?

If every candidate has a cluttered background, more style adjectives won’t help, so change the composition instruction:

Keep all bicycle and workshop objects inside the right two-thirds of the canvas. Leave the entire left third as uninterrupted pale cream. Remove any wall decorations or signs.

This asks for a change you can see, where “make it more professional” leaves the model guessing. Save the candidate you like before continuing.

Prompt for the medium and composition

Name the medium. For a photo, describe viewpoint, light and materials. For an illustration, describe line work, fills, palette and detail. “Flat editorial illustration, dark outlines, four main colors, minimal shading” is more useful than “stunning masterpiece, ultra-detailed, 8K”, and “8K” doesn’t set the file size anyway.

Details help when they remove ambiguity, while repeating the same requirement adds noise. Midjourney’s prompting guide, for example, recommends short descriptive prompts over long instruction lists. A chat editor handles a list of instructions better than a text-to-image model that expects a scene description.

Use references deliberately

A reference can supply composition, identity, style or a specific object, so say which. With a rough sketch:

Use this sketch for placement. Keep the large empty area on the left. Replace the rough bicycle drawing with a flat editorial illustration. The sketch’s colors are only guides and should not appear in the final image.

With two references, give each a role: “Use the layout from image one and the palette from image two” works better than “combine these.”

References influence the result without preserving it exactly. With a real shop’s product photos, compare the output with the original before using it commercially, because a model can invent a part or drop a distinguishing mark.

Separate exploration from finishing

Explore at a cheap, fast setting, then switch to the final quality and resolution once the design works. Higher quality won’t fix a bad layout and can change details, so review the final output again. Keep the accepted image with its prompt and settings, since a chat history makes a poor asset library.

Editing images without starting over

With instruction-based editing you give the model an image and describe the change:

Change the blue bicycle frame to dark green. Preserve the wheel positions, the repair stand, the background, and the empty area on the left.

Then check that everything you asked to preserve survived. If you need the exact original pixels, composite the region in an editor.

Inpainting changes a region selected with a mask, like the toolbox. Mask conventions vary (white for the editable area in some workflows, transparent pixels in some APIs), so follow the docs. Leave some surrounding area so the replacement blends in, and compare with the source, because some APIs treat the mask as a hint and change other regions too.

Outpainting extends the image past its edges, useful if the illustration needs more room for the headline. In some interfaces “make it wider” crops or recomposes instead, so check what you’re getting.

A normal resize interpolates pixels, while a generative upscaler invents detail: extra spokes, a changed logo, texture that wasn’t there. Compare at 100% zoom and at display size. Upscaling a wrong bicycle gives you a more detailed wrong bicycle.

For transparency, remember that a checkerboard drawn into the picture is just pixels. Use the generator’s transparent output if it has one, or a background removal tool, then look for halos and missing thin parts (spokes are a good test). Save as PNG or WebP, since JPEG has no alpha channel.

Creative tools beyond the chat apps

A creative app can save you work even with a model you could use elsewhere, because selecting regions, keeping references next to the canvas and finishing a design all take time.

Midjourney

Midjourney is a hosted product with its own models, built for exploring a visual direction through variations, references and parameters. Learn its own controls instead of copying Stable Diffusion settings.

Its community guidelines say there’s no general API, apart from rare explicitly granted exceptions, so be suspicious of anything sold as a “Midjourney API”. It’s also open by default: the Stealth documentation explains which plans have privacy controls, and that anything posted in a public Discord channel stays public.

Adobe Firefly

Adobe Firefly is an Adobe model family, a creative app, and a set of Firefly Services APIs. The app includes partner models next to Adobe’s, so check which one you picked before relying on a claim about training data. Its appeal is the editing around it if you already finish work in Photoshop, Illustrator or Express.

Ideogram

Ideogram makes image models, an app and an API, with a focus on typography. Try it with short, exact text, and still proofread letter by letter. It also has downloadable weights for one release, and its licensing page separates public noncommercial weights from commercial self-hosting. The inference code’s license doesn’t cover the weights.

Recraft

Recraft has a design app and APIs for raster and vector generation. A raster illustration with smooth shapes is still pixels, while a generated SVG has editable paths. For icons, open the exported SVG and look at the paths, which can be overly complex or contain stray elements. The Studio rights documentation explains how output rights and visibility differ between free and paid plans.

Other products and models

Plenty of other products wrap one or more models. The same questions apply: which model runs, where, and what you can do with the output.

Product or modelWhat it isWorth knowing
LeonardoOwn and external models, with editing, upscaling and custom modelsCompare by specific model and operation
CanvaDesign editor with generationGood when the deliverable is a design, since the Officina wording stays editable
KreaImage models with realtime and enhancement toolsIts current generation model and downloadable model are different
MagnificStock assets, several models, editing, upscaling and workflowsIts homepage says “Freepik is now Magnific”, so tutorials use both names. Its desktop app can still run operations remotely
Grok ImaginexAI’s image API for generation and editingSeparate from Grok’s language models. Download outputs from temporary URLs promptly
Muse ImageMeta’s image model in Meta AI, paired with Muse Spark for planning and photo editsApp availability depends on rollout. On the Meta Model API it’s the hosted muse-image-1.0, for generation and editing. Muse Spark Open, the downloadable release, is a text model
SeedreamByteDance’s image model family, with Dreamina as the appCovers generation, references and editing
Microsoft DesignerGeneration plus layouts and editing, with related features in CopilotDifferent Microsoft products may use different models

Amazon Nova Canvas reached end of life on September 30, 2026, and calls to amazon.nova-canvas-v1:0 fail. You’ll still find it in the older user guide and sometimes in the Bedrock catalog, so read the model card for the ID you plan to call.

What about Claude?

Anthropic says Claude doesn’t directly generate photos or illustrations like a raster image generator. It can analyze images and create visuals through code, like HTML and SVG. It can also run a workflow that calls an external image tool, and then the image model and its billing are separate from Claude.

How I would choose among these tools

For a one-off illustration, I would start with an image product I already pay for and test composition and editing before buying another subscription. A poster needs a workflow where I can finish typography and layout in an editor, and a feature inside an app narrows the list to documented APIs.

For diagrams and interface icons, I would start with SVG or drawing code, so every connection and label is exact and editable, and use a model at most for a sketch.

Run image models on your computer

Local generation means the model runs on your machine from weights you downloaded. There are no per-call fees, but you pay in hardware, electricity, disk space and time, and some models need a license for your use. It pays off when you run lots of experiments, work offline, or want sensitive images to stay on your own hardware.

Choose the software first

SoftwareInterfaceWhere I would start
Draw ThingsNative Apple app with a canvas and model controlsLocal image work on a Mac, iPhone, or iPad
DiffusionBeeMac applicationAn approachable local Stable Diffusion workflow
ComfyUIVisual graph of connected nodesReusable pipelines and model-specific workflows
InvokeLocally hosted creative interfaceCanvas-based editing with supported models
MFLUXTerminal commands and Python APIApple Silicon generation and automation
stable-diffusion.cppInference engine and command-line toolsDeveloper workflows across supported backends
DiffusersPython libraryBuilding or adapting an image pipeline in code

Not every runner loads every model. Draw Things also offers cloud computation and ComfyUI has remote API nodes, so pick local operations all the way through if that’s what you need.

My guide to open-weight models covers weights, quantization and choosing a model. It’s about language models, so for images you need the software above.

Choose a model that fits the workflow

SDXL is still a good family for learning classic text-to-image, thanks to all the tools, checkpoints and adapters around it. Its base model card explains the optional refiner and the license. Stable Diffusion 3.5 is another downloadable family with its own Stability license, described in the specific model card.

FLUX variants, from the older Schnell weights to the Klein releases, differ in license and resource needs. FLUX.2 Klein 4B uses Apache 2.0, while FLUX 3 Image Klein uses the FLUX Kommunity License.

Qwen Image includes generation and editing models. The original Qwen Image and Qwen Image 2.1 have different licenses, and the newer one supports native RGBA output.

Z-Image Turbo is a compact, distilled model supported by MFLUX and ComfyUI. The official project gives its intended sampling settings, and we’ll use it in the terminal example. Ideogram and Krea publish downloadable releases too.

Some downloadable models are server projects. HunyuanImage documents multi-GPU reference configurations, and its license has territorial restrictions.

Understand the memory requirements

On a PC with a discrete GPU, VRAM is the memory on the graphics card, separate from system RAM, so lots of RAM doesn’t save you from running out of VRAM. Apple Silicon uses unified memory shared by the CPU, GPU, system and apps, so you don’t get the whole number on the box.

The main weights are only part of it. The pipeline also loads text encoders, a decoder, image encoders and working memory, and higher resolutions, bigger batches, more references and control models all add more.

Quantization stores some values at lower precision to cut memory. Offloading moves weights or work between memory locations, so a workflow fits but runs slower.

Use the guidance for your runner. Invoke’s requirements, for example, give different numbers for SDXL, FLUX and quantized Z-Image. Before buying a GPU, look for someone running the complete workflow on similar hardware at your target resolution, not at the model’s parameter count.

Local walkthrough with Draw Things

On an Apple device, Draw Things gets you generating without a Python environment.

Install the official app and pick a model from its catalog that suits your device, checking download size, license and required components. Use local generation, since the cloud option runs remotely.

Enter the Officina prompt, pick a landscape canvas, and start with one image and the recommended settings. Save it with its settings, then change one part of the prompt, like the bicycle’s color, and generate again. The same seed helps compare settings, but a changed prompt can still change the whole composition. For a targeted change to the accepted image, use the app’s editing or masked workflow.

If you run out of memory, lower the resolution or pick a lighter model, and run one job at a time.

Local walkthrough with MFLUX on a Mac

MFLUX runs image models on Apple’s MLX framework, and its command line makes jobs easy to save and repeat. You need an Apple Silicon Mac with enough free memory for the model.

Install uv if you don’t have it, then install MFLUX in its own tool environment:

uv tool install --upgrade mflux

The first generation downloads the model files, so leave disk space for them and check any license requirements first.

This command adapts the project’s Z-Image Turbo example to our illustration:

mflux-generate-z-image-turbo \
  --prompt "Flat editorial illustration of one blue city bicycle on the right, small repair stand behind it, pale cream background, dark outlines, limited palette, empty left third, no lettering" \
  --width 1280 \
  --height 768 \
  --seed 42 \
  --steps 9 \
  -q 8

-q 8 selects 8-bit quantization. It’s not the number of images or a quality rating.

The command prints where it saved the image. If it doesn’t fit in memory, check MFLUX’s quantization options.

Keep the command. To explore alternatives, change only the seed. To test a prompt change, keep everything else fixed. Don’t reuse this model’s steps and guidance with an unrelated model, since distilled models have their own intended settings.

Local walkthrough with ComfyUI

ComfyUI shows an image pipeline as a graph where each node loads data, transforms it, runs a model or saves an output. It looks complicated because it shows steps other apps hide, but you can start from a maintained template.

Install and load a supported template

Use the official installation for your operating system and GPU instead of an old tutorial’s list of GPU packages. Then open the workflow templates and pick a text-to-image example your machine can run. To learn the classic workflow, use an SDXL template and follow its download instructions, which say which files it needs and where they go.

ComponentPurpose
Checkpoint or diffusion modelThe main generation weights
Text encoderConverts the prompt into model conditioning
VAE or other image decoderConverts a latent result into image pixels
LoRAAdds a learned adaptation to compatible components
Control modelConditions generation on structure such as edges or depth
UpscalerEnlarges or refines the selected output

Install only what the workflow asks for. A missing text encoder won’t be fixed by downloading another checkpoint.

Follow the image through the graph

A classic SDXL graph has these stages:

A classic ComfyUI text-to-image workflowA checkpoint supplies model components. Prompt conditioning and an empty latent feed a sampler, then a decoder and save node produce the image file.Load checkpointEncode promptSamplerEmpty latent withchosen dimensionsDecode latentsSave image

The template already wires them up. Newer models can need different nodes, separate encoders or different sampling.

Put the Officina prompt in the positive prompt node, keep any negative prompt short, and pick a landscape size within the template’s recommendations with a batch size of one. Run it, and the image appears in the output node. A missing-model error usually means a filename or folder doesn’t match what the template expects.

Save a reproducible workflow

Export the workflow JSON and keep it next to the image. ComfyUI can embed the workflow in output files, but that metadata disappears when an image is converted or uploaded.

Record the model files, seed, dimensions, sampling settings, adapters and references for each accepted asset. To reproduce old results, keep the model files or their hashes, since a new model revision, sampler, library or GPU can change the output even with the same seed.

Keep local workflows local

A paid partner node can send your references to a remote service while ComfyUI runs locally, so inspect the graph before using private images. Custom nodes are executable software: install only the ones you need, from maintainers you trust. The ComfyUI repository documents the offline and API-node controls for built-in features, and third-party extensions you have to review yourself.

The controls you will encounter

Local tools expose more controls than chat products, and they mean slightly different things in different models.

The seed initializes the random process. Keep it fixed while you change one setting, change it to get alternatives. It won’t reproduce the same file across implementations or model revisions, and it won’t keep a character’s identity in a new scene.

Steps are the updates that produce the image. More steps cost more time, and quality stops improving at some point. Distilled models are trained for few steps, so a big step count copied from another model wastes time or makes things worse. The sampler and scheduler decide how the updates proceed. Start with the model’s maintained example and only change them to fix a specific problem.

Guidance sets how strongly the prompt steers generation. Classic classifier-free guidance combines conditioned and unconditioned predictions, and a high value can cause harsh contrast or odd colors. Some models use a different mechanism or expect guidance off, so there’s no universal good number.

A negative prompt lists unwanted features, in models that support it. Keep it short and relevant, because a long inherited list can suppress things you want. Writing “negative prompt” in a chat message does nothing special.

Image strength, in image-to-image workflows, sets how far the result can move from the source. Lower usually keeps more of the input, mistakes included: if the frame is wrong, a low strength keeps it wrong.

ControlNet, adapters, and LoRAs

A ControlNet or similar control model conditions generation on structure like an edge map, depth map or pose. The Diffusers ControlNet guide shows how it works. For Officina, a sketch could fix the wheel positions and frame outline while the generator handles the style.

An image-reference adapter takes visual conditioning from a reference, and different adapters keep different aspects of it. A LoRA is a compact learned adaptation for a style or subject. You can use an existing one or train your own, as the Diffusers training guide explains.

You need to preserveA control to investigate
The composition of a drawingEdges, depth, or another structural control
The appearance of a referenceA supported reference adapter or image editing model
A recurring learned style or subjectA compatible LoRA
Exact lettering and placementText layout or drawing code
Unchanged pixels outside a regionMasked compositing with explicit preservation

All of these are tied to an architecture, so an SDXL LoRA won’t work with FLUX. A high LoRA weight can distort the subject, so test on several prompts before a batch. Only train your own when references and prompting aren’t enough, because it means preparing a dataset, paying for compute and maintaining one more artifact.

Generate images from JavaScript

An API makes sense when generation becomes part of an app or a repeatable workflow. These Node.js scripts use the built-in fetch, file APIs and FormData. Create a working directory:

mkdir officina-images
cd officina-images

Put your API key in the matching environment variable and keep it out of source files and browser code. Every call can cost money, so run one request at a time while you learn the response format.

Generate with OpenAI

We’ll send the brief to the Images API (official endpoint docs) and save the PNG. With OPENAI_API_KEY set, save this as generate-openai.mjs:

import { writeFile } from 'node:fs/promises'

const apiKey = process.env.OPENAI_API_KEY
if (!apiKey) throw new Error('Set OPENAI_API_KEY before running this file')

const model = 'gpt-image-2.5-flare'
const prompt = 'Flat editorial illustration of one blue city bicycle on the right, a small repair stand behind it, pale cream background, dark outlines, limited palette, empty left third, no lettering'

const response = await fetch('https://api.openai.com/v1/images/generations', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${apiKey}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    model,
    prompt,
    n: 1,
    size: '1536x1024',
    quality: 'low',
    output_format: 'png'
  }),
  signal: AbortSignal.timeout(120_000)
})

if (!response.ok) throw new Error(`Image request failed: HTTP ${response.status}`)

const result = await response.json()
const image = result.data?.[0]?.b64_json
if (!image) throw new Error('The response contained no image')

await writeFile('officina.png', Buffer.from(image, 'base64'))
await writeFile('officina.json', JSON.stringify({
  model,
  prompt,
  size: '1536x1024',
  quality: 'low',
  usage: result.usage
}, null, 2))

Run it:

node generate-openai.mjs

The script saves officina.png and a small metadata file, at a supported landscape size we’ll adjust later.

The timeout only limits how long our script waits, so the request can still complete and be billed on OpenAI’s side. If you get an access error, check the model’s availability and your API account, because a ChatGPT subscription doesn’t set up API billing.

Edit the generated image

Now we upload the PNG and ask for a green frame. The edit endpoint takes multipart form data. Save this as edit-openai.mjs:

import { readFile, writeFile } from 'node:fs/promises'

const apiKey = process.env.OPENAI_API_KEY
if (!apiKey) throw new Error('Set OPENAI_API_KEY before running this file')

const bytes = await readFile('officina.png')
const form = new FormData()
form.set('model', 'gpt-image-2.5-sunburst')
form.set('prompt', 'Change only the blue bicycle frame to dark green. Preserve the composition, wheels, repair stand, pale cream background, and empty left third.')
form.set('image', new Blob([bytes], { type: 'image/png' }), 'officina.png')
form.set('output_format', 'png')

const response = await fetch('https://api.openai.com/v1/images/edits', {
  method: 'POST',
  headers: { Authorization: `Bearer ${apiKey}` },
  body: form,
  signal: AbortSignal.timeout(120_000)
})

if (!response.ok) throw new Error(`Image edit failed: HTTP ${response.status}`)

const result = await response.json()
const image = result.data?.[0]?.b64_json
if (!image) throw new Error('The response contained no edited image')

await writeFile('officina-green.png', Buffer.from(image, 'base64'))

Run it after the generation script:

node edit-openai.mjs

Notice that we let fetch set the multipart content type and boundary. Setting Content-Type: application/json would break the request. This edit has no mask, so compare officina-green.png with the source.

Generate with Google’s Gemini API

Google’s current image generation guide uses the Interactions API for its main examples. We’ll call it directly so you can see the request and response. With GEMINI_API_KEY set, save this as generate-gemini.mjs:

import { writeFile } from 'node:fs/promises'

const apiKey = process.env.GEMINI_API_KEY
if (!apiKey) throw new Error('Set GEMINI_API_KEY before running this file')

const model = 'gemini-nano-banana-2.1'
const prompt = 'Create a flat editorial illustration of one blue city bicycle on the right, a small repair stand behind it, pale cream background, dark outlines, limited palette, empty left third, no lettering'

const response = await fetch('https://generativelanguage.googleapis.com/v1beta/interactions', {
  method: 'POST',
  headers: {
    'x-goog-api-key': apiKey,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    model,
    input: prompt,
    response_format: {
      type: 'image',
      mime_type: 'image/png',
      aspect_ratio: '16:9',
      image_size: '1K'
    }
  }),
  signal: AbortSignal.timeout(120_000)
})

if (!response.ok) throw new Error(`Image request failed: HTTP ${response.status}`)

const result = await response.json()
const image = result.steps
  ?.filter(step => step.type === 'model_output')
  .flatMap(step => step.content ?? [])
  .findLast(content => content.type === 'image' && content.data)
if (!image?.data) throw new Error('The response contained no image')
if (image.mime_type !== 'image/png') throw new Error('Expected a PNG image')

await writeFile('officina-gemini.png', Buffer.from(image.data, 'base64'))
await writeFile('officina-gemini.json', JSON.stringify({
  interactionId: result.id,
  model,
  prompt,
  usage: result.usage
}, null, 2))

Run it:

node generate-gemini.mjs

The REST response contains steps, so we look for an image inside a model output step (the SDK’s convenience accessors work differently). The API can return a refusal or text instead of an image, which is why the script checks before decoding.

For conversational follow-ups, pass previous_interaction_id. To edit a local image, send it as an image input with its MIME type and base64 data. Older generateContent examples use a different response shape.

Other APIs and hosted open models

Replicate and fal host models from many developers, and OpenRouter offers image generation in its API. You get several model families from one account without running a GPU server.

A shared API format doesn’t mean every model supports masks, seeds or arbitrary sizes, so read each endpoint’s inputs, license and billing. Some return the image right away, others a job ID or polling URL, and some image URLs expire. If your app uses several services, keep the provider-specific code in a small adapter that surfaces unsupported options instead of ignoring them.

Prepare the image for a website

Treat the generated image as a source asset and make a separate file for the site. We’ll use Sharp to fit the whole illustration into a 1600 × 900 WebP, filling leftover space with a light background.

Install Sharp in the example directory:

npm init -y
npm install sharp

Save this as prepare.mjs:

import sharp from 'sharp'

const input = process.argv[2] || 'officina.png'

await sharp(input)
  .rotate()
  .resize(1600, 900, {
    fit: 'contain',
    background: '#fcfcfa'
  })
  .webp({ quality: 85 })
  .toFile('officina-web.webp')

Run it:

node prepare.mjs officina-green.png

That prepares the edited version. For the Google example, run node prepare.mjs officina-gemini.png. Without an argument it uses officina.png.

fit: 'contain' keeps the whole image and can add padding. With cover, Sharp fills the frame and crops, so check that it doesn’t cut the wheels or the heading area. Resizing adds no detail, so if the source is too small, generate bigger or try an upscaler.

On the page, put a real heading over or next to the image and give the image explicit dimensions. A purely decorative image can have an empty alt. For a meaningful one, describe what readers need to know, not the generation prompt.

Consistent characters, products, and asset sets

If our bicycle appears on several pages, it needs the same frame, wheel proportions and palette each time.

Start from an accepted reference and keep a short list of properties to repeat in every request. For Officina: a dark blue step-through frame, cream tires, a brown saddle, dark outlines, minimal shading. Use a reference workflow or a LoRA when that’s not enough, since a shared seed doesn’t encode identity. Review the set side by side, where differences in scale, lighting or line thickness stand out.

When exact product geometry matters, start from product photos, compositing or a 3D render and let the generator handle only the surroundings.

Pixel art and sprites need extra work

Daniel Ball’s practical guide and the Hacker News discussion around it make the point well: a convincing large preview and a production-ready sprite are different deliverables.

A game asset can need a fixed logical grid, a limited palette, clean transparency, a shared origin point and consistent proportions between frames. Nearest-neighbor scaling avoids blending colors when you shrink an image to sprite size, but it won’t fix uneven pixel clusters, mismatched outlines or a bad pose. Palette reduction can wipe out intentional shading, so review the result after cleanup.

For sprite sheets, count the actual rows and columns before slicing, because the model may not have followed your request. Cropping and resizing each frame on its own makes the character grow and shrink during the animation, so keep one frame canvas, scale and anchor point, and watch it in motion.

For strict retro constraints, a pixel-art tool or procedural drawing is often a better start. A coding model can help write a renderer with a fixed grid and palette, while an artist controls the design.

Cost: measure accepted images

Services bill per image, per token, per megapixel, by GPU time, or through subscription credits.

Billing methodWhat to check
Fixed image priceWhich size and operation that price includes
Size and quality tiersInput charges, output tiers, and edit costs
Image tokensInput images, output token consumption, and quality
MegapixelsRounding, minimum charges, and input-image billing
Compute timeStartup, runtime, and whether idle time is charged
Subscription credits or GPU timeRate for each mode, limits, and overage behavior
Local executionHardware, power, licenses, maintenance, and elapsed time

A simpler scene isn’t necessarily cheaper, though some newer models use variable processing. Smaller dimensions only save money if the model prices them lower, and “make a small thumbnail” in a prompt isn’t an output setting.

For current prices, check OpenAI, Google and BFL directly, since aggregators can charge differently.

The number to calculate is:

Generation cost per accepted asset = total generation spend / accepted assets

Say one model costs $0.01 per attempt and you accept 2 images out of 20 attempts. You spent $0.20, or $0.10 per accepted image. Another costs $0.04 per attempt and you accept 4 out of 5. Same $0.20, but $0.05 per accepted image.

Those figures are made up, but they show why you count attempts. Track editing time too: a cheap image that needs twenty minutes of cleanup can end up expensive.

Build image generation into an application

Our scripts make one request and save one file. A user-facing service needs to manage a longer-running job:

An image generation job in an applicationThe server validates a request and creates a queued job. A worker calls the provider, stores the result, and updates the status. Failures lead to an explicit error state.User requests animageServer checks inputand budgetCreate a queued jobWorker calls imageproviderStore output andmetadataMark job completeRecord refusal orfailureClient displays theresultClient displays theerror

Return a job ID right away and let the client follow its status. Some platforms let you handle a generation inside one long request, but a queue helps when the work must survive client disconnects or be rate-limited.

Keep provider keys on the server, and limit who can generate, how many jobs they can queue and how much they can spend.

Be careful with timeouts. The provider may have finished and billed a request even if your connection dropped, so a blind retry can pay for the same image twice. Use the provider’s idempotency or job lookup where it exists. Retry transient errors with bounded backoff (the exponential backoff calculator shows the wait before each attempt, including jitter so a pile of jobs retries across a spread of times), and handle refusals and invalid parameters differently from outages.

Before marking a job complete, check that the image exists with the expected format and dimensions, and copy it to your storage if the URL is temporary. Log the model identifier, operation, settings, output dimensions, duration, usage and status, and keep prompts and references only under a retention policy that fits the data.

A request hash finds identical inputs, but a user asking for another variation expects a new image, so caching is a product decision. In tests, use fixed image responses so your job state tests don’t buy new images on every run.

Let a coding agent use an image model

A coding agent can turn a brief into a prompt, call an image tool, look at the result and revise it. It needs an actual image generation tool for that, which you can provide through a script, a function tool or an MCP server. The guide to building an MCP server explains how tools expose operations to compatible clients.

Give the agent an output directory and a budget:

Create up to four candidate illustrations for the Officina brief. Save each candidate separately. Stop after four paid generation calls. Inspect the images for bicycle geometry and clear space on the left, then show me the candidates and describe any visible problems. Keep the originals.

A call limit works when every call costs about the same. With expensive edits, upscaling or several models, enforce a spending limit in the code that runs the tools.

The agent also needs an image-reading tool to see the pixels, a file path isn’t enough. Check dimensions, format, alpha and file existence with code, and leave composition and whether the bicycle makes sense to visual review. I would keep the final choice for myself: the agent proposes, I decide which image goes on the page.

Licenses, privacy, and provenance

Downloadable weights can come with commercial restrictions, revenue thresholds, territorial limits, or conditions on redistribution and training. The runner’s license is separate, and an MIT-licensed app can load a model with restrictive terms. Read the model card and license for the exact variant, and for commercial work read the hosted provider’s terms too. Whether a license lets you use an image and whether you own exclusive rights to it are different questions.

A photo uploaded to a hosted editor leaves your machine, so check retention, training use, visibility and deletion for your account and endpoint. “Private project”, “not used for training” and “not retained” are three different policies. Locally, model downloads, remote prompt expansion, cloud nodes, extensions and synced output folders can all reach the network even when the generator runs on your machine.

Keep the original references, accepted generations, edit history and licenses. C2PA Content Credentials can record signed provenance information, though they don’t prove the scene is real, and a file without them may or may not be generated. Watermarks are a different mechanism: Google’s image guide documents SynthID for its images, and other vendors do their own thing. Resizing, recompressing or stripping metadata can break provenance records, so check what your export pipeline keeps.

For a first experiment, keep it small: make one Officina illustration, save its settings, change the frame color, and prepare the result for a page. To compare tools, repeat the same exercise through a second route.

Want me to talk about your product? You can sponsor this site.

Related posts about ai: