商业产业 Hacker News AI 2026-10-20T19:22 正文 ok 5702 words

AI 图像生成实用指南:从扩散模型到 Agent

英文标题:🔶 A practical guide to AI image generation, from diffusion to agents

本文是一份面向开发者与实践者的 AI 图像生成实战指南,梳理了从早期 GAN、扩散模型到原生多模态大模型的演进脉络与底层原理。文章详细对比了主流架构(如 Flux、SDXL 与 GPT Image、Gemini)在计费机制、指令遵循、种子可复现性等维度的差异,并系统盘点了图像生成的典型任务与使用路径。作者指出,现代图像生成正从单纯提示词调试走向结合 Agent 与常规代码的混合工程化流水线,为构建高可控性图像应用提供了清晰参考。
打开原文相关 6 · 机会 1
media161.0 KB media259.5 KB media236.5 KB media194.3 KB media267.1 KB media300.2 KB media7.3 KB media83.4 KB media144.6 KB media45.7 KB media94.1 KB

中文

AI 生成的 20 世纪 90 年代风格汉堡与奶昔海报

这是我的 AI 图像生成通用指南。它从宏观层面介绍了模型的工作原理、不同类型、一段简要历史、你在 ChatGPT 中实际得到的内容,以及如何将图像生成集成到你自己的代码和 Agent 中。

本指南基于我自己使用 Python 调用少数几家图像提供商 API 的实验。我生成过像素艺术精灵图、逼真的牌桌背景以及一系列餐厅海报,试图做出一张看起来不像 AI 生成的海报。这里的每张图片都来自那些实验。

TLDR

  • 大多数模型从噪声开始并逐步清理噪声。 文本引导着清理过程。更新的模型还会像对话模型规划句子一样规划图像。
  • 你是在为画布付费,而不是为内容付费。 一张普通的木桌和一张同样尺寸、细节丰富的油画花费相同。
  • ChatGPT 是最容易上手的途径,也是最难控制的。 非常适合单次出图和对话式编辑。对于任何可复现的需求,你需要使用 API。
  • 价格差异超过 100 倍。 从每张图不足 1 美分到 15 美分甚至更多不等。在本地运行模型不花钱。最便宜的模型并不总能带来成本最低的结果。
  • API 变化莫测。 模型可能提前数周通知即遭停用,提供商也会悄悄忽略参数。请将模型配置在外部,并记录你实际收到的返回内容。
  • 描述它是如何制作的,而不是它应该看起来如何。 描述相机、胶片、印刷工艺和构图,比“惊艳”(stunning)和“超高细节”(hyper-detailed)能获得更好的效果。
  • 拼写和手指不再是露馅的破绽。 现在的破绽是品味。过多的光晕、过于夸张的戏剧感,以及不被允许存在的平淡。
  • Agent 很擅长做这件事。 多模态 Agent 可以撰写提示词、调用 API、查看结果并再次尝试。你仍然需要提供品味指导。
  • 最佳流水线是混合架构。 让 AI 处理创意部分,让常规代码处理尺寸、透明度、调色板和布局等精确部分。

简要发展史

  • 2014 - GAN。 Ian Goodfellow 的生成对抗网络(GAN)让两个网络相互对抗,一个生成图像,另一个识别真伪。它们随后生成了令人信服的合成人脸,但很难用文本进行控制。
  • 2015 - DeepDream。 Google 的 Inceptionism 实验颠倒了图像识别过程。由于该网络是在包含大量狗的数据集上训练的,其结果充满了迷幻的狗脸。
  • 2018 - AI 艺术品走上拍卖台。 一幅名为《埃德蒙·德·贝拉米》(Edmond de Belamy)的模糊 GAN 肖像画在佳士得以 432,500 美元成交,超过其最高估价的 40 倍。
  • 2019 - 不存在的人脸。 NVIDIA 的 StyleGAN 为 thispersondoesnotexist.com 提供支持,每次刷新都会生成一张逼真的虚拟人脸。
  • 2021 - DALL-E。 OpenAI 将文本与图像关联在单一模型中。用户可以输入一句话并得到一张图片,即使那张图片很小、很模糊。
  • 2022 - 爆发之年。 DALL-E 2、Midjourney 和 Stable Diffusion 在短短几个月内相继登场。Stable Diffusion 采用开源模式,只需约 7GB 显存的游戏 PC 即可运行,由此催生了庞大的微调模型与工具社区。
  • 2023 - 照片级真实感与可读文本。 一张由 Midjourney 生成的教皇身穿白色羽绒服的照片骗过了大部分互联网用户。Ideogram 推出,带来了真正清晰可辨的文字生成能力。SDXL 提高了开源模型的标杆,而 DALL-E 3 接入了 ChatGPT。
  • 2024 - Flux 与反弹。 前 Stable Diffusion 研究人员创立了 Black Forest Labs 并发布了 Flux,它成为了默认的开源权重模型。与此同时,AI 海报成为了偷工减料的代名词(下文将作更多探讨)。
  • 2025 - 原生图像模型。 OpenAI 将图像生成能力直接集成到 GPT-4o 中,“吉卜力工作室”风格在社交媒体上引发风潮。Google 的 Gemini 2.5 Flash Image(代号 Nano Banana)以每张图 0.039 美元的超低成本推动了对话式编辑普及。
  • 2026 - 资源富余与频繁更迭。 OpenAI 在其 API 中停用了 DALL-E。仅 OpenRouter 就列出了来自十几家公司的 60 多种图像模型,旧模型每隔几个月就会被淘汰。

六根手指与乱码文字

有那么几年,辨别 AI 图像最简单的方法就是数手指,或者试图辨认背景里的路牌。现在这两个问题大多已经得到解决,而它们最初产生的原因很大程度上揭示了这些模型的工作原理。

手部结构复杂、灵活多变,在照片中通常会被部分遮挡——藏在杯子后面、放在口袋里或位于画框外。模型学到的是手作为一种纹理的模样,而不是它具体有多少根手指。2023 年 3 月发布的 Midjourney v5 被广泛报道为转折点。专家推测这是由于经过更优质筛选、包含更清晰手部图像的训练数据,加上更大的模型体量和更高的分辨率所致。但这并非完美无缺,背景中的人物在随后一段时间里仍然会出现怪异的肢体结构。

文本的问题则有更具体的技术诱因。大多数模型通过分词器(Tokenizer)来读取你的提示词,分词器将提示词拆分为单词词元片段而不是字母。模型根本看不到“chocolate”是由 c-h-o-c-o-l-a-t-e 拼写而成的,因此无法逐个字母绘制它。2022 年发表的一篇论文《Character-Aware Models Improve Visual Text Rendering》(基于字符感知的模型改善视觉文本渲染)表明,让模型能够访问单个字符可以解决大部分问题。团队成员包括前 Google Imagen 研究人员的 Ideogram 在 2023 年将文本渲染作为核心卖点。到 2025 年,来自 OpenAI 和 Google 的多模态模型对语言的理解与任何聊天机器人无异,已经让生成可读文本变得十分普遍。

这些修复是分阶段落地的,而在此期间,旧问题屡屡渗透进现实世界。2024 年 2 月在格拉斯哥举办的“威利巧克力体验展”(Willy's Chocolate Experience)在由 AI 生成的海报上打出了“a pasadise of sweet teats”(把 paradise 拼错)的滑稽标语。长文本问题至今仍未彻底解决。2025 年 12 月,Nano Banana Pro 在一张制作精良的信息图表中,将“Lived experience”渲染成了 Liver experience。

工作原理

用好这些模型并不需要掌握高深的数学原理,但确实需要对其运行机制有一个大致印象,因为这能解释大部分意料之外的情况。

扩散模型

大多数图像模型都是扩散模型。在训练阶段,它们学习如何消除图像中的噪声。取一张照片,添加一些静电噪声,然后训练模型预测原本干净的版本是什么样。这一过程在不同噪声级别上重复数十亿次。

在生成图像时,流程反向进行。从纯静电噪声开始,分多步每次去除一点噪声。文本编码器将你的提示词转换为数值,引导每一步朝着符合提示词的图像方向演进。

在实践中有两个至关重要的细节:

  • 它们在压缩空间中工作。 潜在扩散(Latent Diffusion)是 Stable Diffusion 背后的核心方法,它在一个各边比最终图像小约 8 倍的内部画布上作画,然后将其解码。这就是为什么你无法要求生成 32 x 32 的图像,因为其内部画布大小会变成 4 x 4。
  • 步数产生成本与耗时。 步数越多速度越慢,在达到特定阈值之前通常效果也越好。像 Flux Schnell 和 SDXL Turbo 这样的“蒸馏”模型经过专门训练,仅用 4 步即可生成优质图像,而不是常规的 30 步。

多模态模型

较新的方法是将图像生成能力内置在同样具备语言理解能力的模型中。由于是由同一个模型阅读你的提示词并生成画面,它们在指令遵循、文本渲染以及根据所给参考图进行编辑方面的表现要出色得多。GPT Image、Gemini 系列图像模型以及 Meta 的 Muse 都采用这种方式运作。有些模型在绘图前会先对布局进行推理,当提示词涉及物体摆放位置时,这种机制会带来显著差异。

多模态模型的底层也是扩散模型吗?

通常部分是,而且两者之间的界限比营销宣传中呈现的要模糊得多。

OpenAI 关于 GPT-4o 图像生成的发布博文暗示这是一种混合架构:由语言模型像撰写句子一样将图像规划为词元序列,再由扩散风格的解码器绘制最终像素。Google 尚未公开发布其 Gemini 图像模型的工作原理。反过来,像 Flux 这样的现代“扩散”模型也使用了同时处理文本和图像的 Transformer 模块,这与对话模型的构建模块相同。

因此,有价值的区分点并非数学原理,而是图像模型对语言理解的深度。正是这种理解力带来了指令遵循、可读文本渲染和编辑能力。

两者对比

扩散模型(Flux、SDXL、Imagen) 多模态模型(GPT Image、Gemini、Muse)
成本驱动因素 分辨率与采样步数
优势 成本低、速度快、可本地运行、利用种子可复现结果
劣势 对提示词的理解更字面化
是否开源权重? 通常开源

种子(Seeds)

扩散模型起始阶段的随机静电噪声是由种子(Seed)决定的。相同的种子、相同的提示词和相同的模型会产生完全相同的图像。这是实现批次生成可复现的基础。许多托管服务模型会忽略或拒绝种子参数,因此在构建依赖种子的系统之前,务必先行确认验证。

图像生成的类型

“图像生成”涵盖了若干种不同的任务类型。大多数提供商只支持其中的一部分,而非全部。

类型 功能 示例用途 在哪里尝试
文本生成图像(Text to image) 提示词转化为图像 概念艺术、背景图、占位素材 OpenAI 图像指南
图像编辑(Image editing) 在现有图像上执行“把马克杯变成蓝色” 在设计上进行迭代而无需重新生成 Gemini 图像文档
参考图生成(Reference images) 保持角色、产品或风格一致 同一骑士的八种不同姿势 Gemini 图像文档
局部重绘(Inpainting) 重新生成特定的蒙版区域 移除物体、修复手部破绽 Diffusers 局部重绘指南
画面外扩(Outpainting) 将图像扩展到边缘之外 将正方形图像扩展为横幅 ComfyUI 内绘与外扩示例
结构控制(Structural control) 依照草图、姿态或深度图进行生成 精确契合排版与构图 ControlNet 与 Diffusers ControlNet 指南
微调(LoRA) 教会开源模型新风格或特定主体 打造一致的企业专属视觉风格或产品图 Diffusers LoRA 训练
图像超分辨率(Upscaling) 在更高分辨率下补充细节 对低成本批次生成中的精选素材进行精修 Real-ESRGAN
背景去除(Background removal) 抠出主体 透明背景精灵图和产品白底图 rembg
文本生成矢量图(Text to vector) 生成 SVG 格式而非像素位图 图标与 Logo 标志 Recraft

可平铺纹理(Tileable textures)值得特别一提。基础模型通常无法稳定产出无缝平铺贴图。你需要专门为此训练的模型或 LoRA,或者工具中提供的平铺(tiling)专用选项。

使用途径

途径 示例 适用场景 注意事项
对话应用 ChatGPT、Gemini 单次出图、快速编辑 控制力有限、存在使用限额
创意工具 Midjourney、Adobe Firefly、Canva 设计师、高完成度作品 订阅制付费、难以实现自动化
原生 API OpenAI、Google Gemini 和 Vertex AI 生产环境代码 各家 API 规范互不相同
聚合平台 OpenRouter、Replicate、fal.ai 用一个密钥尝试多种模型 部分服务存在溢价加成,模型上下架频繁
本地运行 配合 mflux 或 ComfyUI 运行 Flux 或 SDXL 免费批量生成、完全掌控权 需要较强的独立 GPU 或 Apple Silicon 支持,生成速度相对较慢

在我的实验中,我特意避开了订阅制服务。所有操作要么是通过 API 按张付费,要么是在本地设备上直接运行。

你在 ChatGPT 中得到的是什么

大多数人初涉 AI 图像生成的经历,都是在 ChatGPT 中输入一段提示请求。了解你实际使用的是什么非常有必要。

ChatGPT 使用的是 OpenAI 自研的 GPT Image 模型,自 2025 年 3 月起该功能就已内置在对话功能中。

DALL-E 还是主力吗? 并非如此。从 2023 年 10 月到 2025 年 3 月切换为 GPT-4o 图像生成之前,DALL-E 3 曾是 ChatGPT 的底层图像生成器。随后 OpenAI 于 2026 年 5 月 12 日从其 API 中移除了 DALL-E 2 和 3。现在该名称更多是作为 AI 图像代名词存续。

它的强项:

  • 对话交互。 你可以说“光线更暖一些”或者“把马克杯往左边移一点”……

English / 原文

A 1990s style burger and shake poster generated by AI

This is my general guide to AI image generation. It covers how the models work at a high level, the different types, a bit of history, what you actually get from ChatGPT and how to build image generation into your own code and agents.

It’s based on my own experiments calling a handful of image providers from Python. I’ve generated pixel art sprites, photorealistic card table backgrounds and a series of restaurant posters, trying to make one that doesn’t look AI generated. Every image here came from those experiments.

TLDR

  • Most models start from noise and clean it up. Text steers the clean-up. Newer models also plan the image the way a chat model plans a sentence.
  • You pay for the canvas, not the content. A plain wooden table costs the same as a detailed oil painting of the same size.
  • ChatGPT is the easiest way in and the hardest to control. Great for one-off images and conversational edits. For anything repeatable you want the API.
  • Prices vary more than 100 times over. Anything from a fraction of a cent to 15 cents or more per image. Running a model locally costs nothing. The cheapest model isn’t always the cheapest result.
  • The APIs move under you. Models get retired with weeks of notice and providers quietly ignore parameters. Keep the model in config and log what you actually got back.
  • Describe how it was made, not how it should look. Camera, film, print process and layout get better results than “stunning” and “hyper-detailed”.
  • Spelling and fingers are no longer the giveaway. The tell now is taste. Too much glow, too much drama and nothing allowed to be plain.
  • Agents are good at this. A multimodal agent can write the prompt, call the API, look at the result and try again. You still need to supply the taste.
  • The best pipelines are hybrids. Let AI do the creative part. Let ordinary code do the exact parts like sizing, transparency, palettes and layout.

A short history

  • 2014 - GANs. Ian Goodfellow’s generative adversarial networks pit two networks against each other, one making images and one spotting fakes. They go on to produce convincing synthetic faces but are hard to steer with text.
  • 2015 - DeepDream. Google’s Inceptionism experiments turn image recognition inside out. The results are full of psychedelic dog faces, because the network was trained on a dataset with a lot of dogs in it.
  • 2018 - AI art goes to auction. A blurry GAN portrait called Edmond de Belamy sells at Christie’s for $432,500, more than 40 times its top estimate.
  • 2019 - Faces that don’t exist. NVIDIA’s StyleGAN powers thispersondoesnotexist.com, a new photorealistic fake face on every refresh.
  • 2021 - DALL-E. OpenAI links text and images in one model. You can type a sentence and get a picture, even if it’s a small, blurry one.
  • 2022 - The breakout year. DALL-E 2, Midjourney and Stable Diffusion all arrive within a few months. Stable Diffusion is open source and runs on a gaming PC with about 7GB of video memory, which kicks off a huge community of fine-tunes and tools.
  • 2023 - Photorealism and readable text. A Midjourney image of the Pope in a white puffer jacket fools much of the internet. Ideogram launches with text you can actually read. SDXL raises the bar for open models and DALL-E 3 moves into ChatGPT.
  • 2024 - Flux and the backlash. Former Stable Diffusion researchers found Black Forest Labs and release Flux, which becomes the default open-weight model. Meanwhile AI posters become a byword for cutting corners (more on that below).
  • 2025 - Native image models. OpenAI builds image generation into GPT-4o itself and the “Studio Ghibli” trend takes over social media. Google’s Gemini 2.5 Flash Image, nicknamed Nano Banana, makes conversational editing cheap at $0.039 an image.
  • 2026 - Abundance and churn. OpenAI switches off DALL-E in its API. OpenRouter alone lists over 60 image models from a dozen companies, and older models are retired every few months.

Six fingers and gibberish text

For a few years the easiest way to spot an AI image was to count the fingers or try to read the sign in the background. Both problems are now mostly fixed, and why they happened says a lot about how these models work.

Hands are complicated, flexible and usually partly hidden in photos. Behind a cup, in a pocket, half out of frame. The models learned what hands look like as a texture, not how many fingers they have. Midjourney v5 in March 2023 was widely reported as the turning point. Experts suspected better curated training data with clearer pictures of hands, plus bigger models and higher resolutions. It wasn’t perfect. People in the background still had alien anatomy for a while.

Text had a more specific cause. Most models read your prompt through a tokeniser that splits it into chunks of words rather than letters. The model never sees that “chocolate” is spelled c-h-o-c-o-l-a-t-e, so it can’t draw it letter by letter. A 2022 paper, Character-Aware Models Improve Visual Text Rendering, showed that giving the model access to individual characters fixed much of the problem. Ideogram, whose team included former Google Imagen researchers, made text a selling point in 2023. By 2025 the multimodal models from OpenAI and Google, which understand language as well as any chatbot, had made readable text normal.

The fix arrived in stages, and the old problem leaked into the real world in between. The Willy’s Chocolate Experience in Glasgow in February 2024 promised “a pasadise of sweet teats” on its AI-generated posters. Long text is still not completely solved. In December 2025 Nano Banana Pro turned “Lived experience” into Liver experience on an otherwise good infographic.

How it works

You don’t need the maths to use these models well. You do need a rough picture of what’s going on, because it explains most of the surprises.

Diffusion models

Most image models are diffusion models. During training they learn to remove noise from images. Take a photo, add a bit of static and teach the model to predict what the clean version looked like. Repeat billions of times at every level of noise.

To generate, you run that in reverse. Start from pure static and remove a little noise at a time over a number of steps. A text encoder turns your prompt into numbers that steer each step towards an image that matches it.

Two details matter in practice.

  • They work in a compressed space. Latent diffusion, the approach behind Stable Diffusion, paints on an internal canvas about 8 times smaller on each side than the final image, then decodes it. That’s why you can’t ask for a 32 x 32 image. The internal canvas would be 4 x 4.
  • Steps cost time. More steps is slower and usually better, up to a point. “Distilled” models like Flux Schnell and SDXL Turbo are trained to get a good image in 4 steps instead of 30.

Multimodal models

The newer approach puts image generation inside a model that also understands language. Because the same model reads your prompt and makes the picture, these are much better at following instructions, rendering text and editing an image you give them. GPT Image, Gemini’s image models and Meta’s Muse work this way. Some reason about the layout before they draw, which makes a real difference when the prompt is about where things go.

Are multimodal models diffusion under the hood?

Often partly, and the line is blurrier than the marketing suggests.

OpenAI’s launch post for GPT-4o image generation hints at a hybrid. A language model plans the image as a sequence of tokens, the same way it writes a sentence, and a diffusion-style decoder paints the final pixels. Google hasn’t published how its Gemini image models work. Going the other way, modern “diffusion” models like Flux use transformer blocks that process text and image together, which is the same building block as a chat model.

So the useful distinction isn’t the maths. It’s how deeply the image model understands language. That’s what gives you instruction following, readable text and editing.

How they compare

| Diffusion models (Flux, SDXL, Imagen) | Multimodal models (GPT Image, Gemini, Muse)
---|---|---
What drives cost | Resolution and number of steps | Image tokens, usually fixed per size and quality tier
Strengths | Cheap, fast, runs locally, seeds give repeatable results | Follows instructions, renders text, edits from a reference image
Weaknesses | Takes prompts more literally | Often no seed control and a fixed output size
Open weights? | Often | Rarely

Seeds

The random static a diffusion model starts from is set by a seed. Same seed, same prompt and same model gives the same image. That’s what makes batches repeatable. Many hosted models ignore or reject seeds, so don’t build anything that depends on them without checking.

Types of image generation

“Image generation” covers several different jobs. Most providers support some of them, not all.

Type What it does Example use Where to try it
Text to image A prompt becomes a picture Concept art, backgrounds, placeholders OpenAI image guide
Image editing “Make the mug blue” on an existing image Iterating on a design without starting again Gemini image docs
Reference images Keep a character, product or style consistent The same knight in eight poses Gemini image docs
Inpainting Regenerate one masked area Removing an object, fixing a hand Diffusers inpainting guide
Outpainting Extend an image past its edges Turning a square into a banner ComfyUI inpaint and outpaint examples
Structural control Follow a sketch, pose or depth map Matching a layout exactly ControlNet and the Diffusers ControlNet guide
Fine-tuning (LoRA) Teach an open model a new style or subject A consistent house style or product Diffusers LoRA training
Upscaling Add detail at a higher resolution Finishing the keepers from a cheap batch Real-ESRGAN
Background removal Cut the subject out Transparent sprites and product shots rembg
Text to vector Produce SVG instead of pixels Icons and logos Recraft

Tileable textures are worth a special mention. Plain models won’t reliably produce a seamless tile. You need a model or LoRA trained for it or a tiling option in the tool.

Ways to use it

Route Examples Good for Watch out for
Chat apps ChatGPT, Gemini One-off images, quick edits Little control, usage limits
Creative tools Midjourney, Adobe Firefly, Canva Designers, polished results Subscriptions, hard to automate
Direct APIs OpenAI, Google Gemini and Vertex AI Production code Each API is different
Aggregators OpenRouter, Replicate, fal.ai Trying many models with one key Markup on some, models come and go
Local Flux or SDXL with mflux or ComfyUI Free bulk generation, full control Needs a decent GPU or Apple Silicon, slower

For my experiments I deliberately avoided subscriptions. Everything was pay per image through an API or was run locally.

What you get with ChatGPT

Most people’s first experience of AI images is typing a request into ChatGPT. It’s worth knowing what you’re getting.

ChatGPT uses OpenAI’s own GPT Image models, which have been built into the chat since March 2025.

Is DALL-E still a thing? Not really. DALL-E 3 was ChatGPT’s image generator from October 2023 until the switch to GPT-4o image generation in March 2025. OpenAI then removed DALL-E 2 and 3 from its API on 12 May 2026. The name lives on mostly as a generic term for AI images.

What it’s good at

  • Conversation. You can say “warmer light” or “move the mug to the left” and it edits rather than starting again.
  • Following instructions. It handles layouts, counts and specific details better than older diffusion models.
  • Text. Signs, posters and labels are usually spelled correctly.
  • References. Upload a photo or sketch and it will work from it.

What it’s not good at

  • Control. You get a handful of aspect ratios, no seed and no way to run a batch. You can’t ask for exactly 1024 x 512.
  • Speed. Each image takes anything from a few seconds to over a minute, and there are usage limits, especially on the free plan.
  • A house style. Left to itself it drifts towards a recognisable look. The 2025 model was known for a warm yellow cast nicknamed the “piss filter”. Describe the lighting and colour you want rather than leaving it to the defaults.
  • Content rules. It will refuse some requests, like real public figures in certain contexts or close copies of copyrighted characters.

Provenance. OpenAI adds both C2PA metadata and a SynthID watermark to its images, according to its help centre. Google adds SynthID to Gemini images. Metadata is easily stripped. Watermarks survive screenshots better, but neither is something to rely on if you’re trying to prove an image is or isn’t AI.

ChatGPT or the API? The same family of models is available through OpenAI’s API, billed per image. The API gives you sizes from a fixed list, quality tiers, batching from code and, on some models, transparent backgrounds. If you need more than a few images or the same kind of image repeatedly, use the API. The Gemini app versus the Gemini API is the same story.

What it costs

You pay for the canvas

The most useful mental model I’ve found is a painter who only owns large canvases, charges per canvas and paints every square inch with the same care. A flat blue sky gets as much effort as a face.

  • Simple pictures aren’t cheaper. The model does the same work across the whole image.
  • You can’t generate small. Models work best at roughly 512 to 1600 pixels. For a 32 x 32 sprite you generate a big picture and shrink it yourself.
  • Asking for a smaller size often doesn’t help. Gemini 2.5 Flash Image returned 1024 x 1024 when I asked for 512 x 512, at the same flat price.

Four ways you get billed

Billing model Examples Does making it smaller help?
Flat per image Gemini 2.5 Flash Image, Recraft No
Per image, tiered by size and quality Newer Gemini models, OpenAI GPT Image A little. The smallest tier is still a big canvas for most needs
Per megapixel or GPU-second Replicate, fal.ai, most models on OpenRouter Yes
Your own electricity Local Flux Yes, and speed is what you save

The landscape in October 2026

Option Approx. cost per image Good for
Local Flux Schnell $0, plus your time Prompt iteration and bulk batches
Flux Schnell on Replicate or fal.ai ~$0.003 Cheap cloud batches
Recraft V4.1 Flash (OpenRouter) ~$0.007 Fast, cheap illustration
FLUX 3 Image Klein (OpenRouter) ~$0.009 Cheap photorealism
Meta Muse Image (OpenRouter) ~$0.01 Following layout instructions
OpenAI GPT Image 1.5 $0.009 low, $0.034 medium, $0.133 high Quality tiers from OpenAI
Gemini 2.5 Flash Image ~$0.039, half that via the Batch API Edits and character consistency
Nano Banana 2 (Gemini 3.1 Flash Image) ~$0.045 at 0.5K, ~$0.067 at 1K Newer and better, but pricier
Nano Banana Pro (Gemini 3 Pro Image) ~$0.134 and up Final, hero quality

Newer flagship models cost more per image, not less. The cheap end of the market is open-weight models and the “flash”, “mini” and “lite” tiers. OpenRouter’s model list is the easiest place to compare current prices.

Cost per usable image

Price per image is misleading on its own.

cost per usable image = (price per image x attempts needed) + your time

A free local model that needs ten tries can lose to a $0.039 model that gets it right first time. In my experiments a model costing a tenth of a cent more saved several retries.

Prompting that works

Describe the camera, not the view

I wanted card table backgrounds for a game. That meant a table seen from directly above, with a few props around the edges and the middle kept clear for the cards.

Asking for “seen from directly above” isn’t enough. Models learn from photos, and most photos of tables are taken at an angle. My first attempts came back as nice pictures of a table with a wall and a window behind it, and every prop bunched down one side.

Two changes fixed most of it.

  1. Describe the camera. “An orthographic flat lay. The camera points straight down at the tabletop, with no perspective and no horizon. The table surface fills the entire frame edge to edge. No walls, windows, floor or table edges are visible.”
  2. Spell out where things go. “Spread these around all four edges (top, bottom, left and right) rather than bunching them on one side or in the corners. Each one should be small and cut off by the edge of the image.”

Here’s the same prompt for a pine kitchen table, with both changes, run on two different models.

Pine kitchen table from FLUX 3 Klein with props bunched into the corners FLUX 3 Klein, $0.009 Pine kitchen table from Meta Muse with props cropped at each edge Meta Muse, $0.01

Both are now top-down. Flux leaves the props whole and pushes them into the corners. Muse, a multimodal model that plans before it draws, put one item on each edge, cropped each one and left the middle clear.

Marketing is where AI images get the most criticism

Posters, flyers and infographics are one of the most common uses for AI images. They’re also where it most often looks cheap.

  • Gibberish. The Willy’s Chocolate Experience posters promised “catgacating” and “exarserdray”. The event itself was so far from the pictures that people called the police.
  • Poor taste. A24’s AI-generated posters for Civil War drew criticism in April 2024. They showed scenes that weren’t in the film and had obvious errors, like a Chicago landmark rebuilt at the wrong angle. A lot of the anger was about a studio using AI instead of paying artists.
  • Getting the facts wrong. Karen Spinner’s experiment turning a research paper into an infographic is a great example with all the prompts and source material shown. It ended up “basically usable”, but only after six attempts. Along the way the model misspelled words, ignored instructions about white space and filled every gap it could find. Her advice was to keep the copy short and proofread everything.

Making a poster that doesn’t look AI generated

So I set myself a challenge. Could I get a restaurant poster that wouldn’t immediately read as AI?

My first attempt was a screenprinted trattoria poster. Rather than describe a look, I described a real print process. Three flat spot inks on uncoated paper, slight misregistration, a scan rather than a photo, an off-centre composition and a named design tradition.

Screenprint style poster for Trattoria Bruno with a linocut plate of spaghetti and a wine carafe

The ink texture is convincing. The bottom line looks like clean digital type but the fold crease may be too much.

The harder test was a 90s burger poster with food that looks real. AI food tends to be too perfect, too glossy and too heroic. I asked for 1996 medium-format film photography and deliberately imperfect food, like a lopsided bun and pale winter tomatoes.

Retro burger poster with comic lettering and a scuffed finish Retro pastiche Clean corporate burger, shake and fries poster on a red background Deliberately boring Over the top AI style burger poster with flames, a galaxy and chrome lettering Maximum AI slop

The first version had good food but read as a “vintage” filter, with comic lettering and fake wear. A real 1996 poster would have been clean and corporate.

So I made it boring. A pristine scan, a flat red background with an early-Photoshop gradient, a cut-out product photo with a slightly too-even drop shadow and plain heavy italic type. I even asked for it to look “competent and slightly bland”. That one would pass a casual glance. It isn’t quite 1996, because the shake is in a dome-lid cup you’d see in the 2010s.

For balance, I asked for the worst version I could think of. Unreal Engine, volumetric god rays, floating ingredients and a galaxy. It delivered a dozen patties with no bottom bun, a shake with no glass and fries raining down from space. Unlike AI posters of old (2 years ago), the spelling is correct these days. The giveaway now isn’t the spelling. It’s that every element is turned up to the maximum.

Prompting rules of thumb

  • Describe how it was made. Camera position, lens, film stock, lighting setup, print process and scanning give the model concrete constraints.
  • Leave out the hype words. “Stunning”, “hyper-detailed” and “8K” push the model straight into the AI look.
  • Be blunt about layout. Say what fills the frame, what’s visible and where each element goes.
  • Real design is restrained. Asking for “slightly bland” did more than any amount of detail.
  • Keep text short and exact. Put it in quotes, add “spell every word exactly as written” and proofread the result.
  • Name period details. The model drifts towards modern objects unless you name the old ones.
  • Generate two or three. Variants of the same prompt often differ a lot, and at a cent each, it’s cheaper than rewriting the prompt.
  • Lock in a template for batches. Use the same style suffix and seed for every asset in a category so the batch doesn’t drift.

Pixel art

Pixel art and game sprites look like the easiest possible job for an image model. Small, simple, few colours. They turn out to be a great example of how AI image generation is harder than you would expect.

Pixel art is a style, not a format

A pixel art knight on Gemini 2.5 Flash Image took 7.5 seconds and cost about $0.039. It’s clean and convincingly retro.

8-bit pixel art knight with a sword and shield

But it’s a 1024 x 1024 picture of a sprite, not a sprite. The “pixels” wobble in size, don’t sit on a true grid and use far more colours than a real palette. The “solid” magenta background was a slightly varying pink. To make it usable I still had to remove the background, snap it to a grid with nearest-neighbour downscaling and reduce the palette, all in ordinary code.

Many sprites on one canvas

If you pay per canvas and a sprite only needs a fraction of it, the obvious move is to ask for many sprites in one image. It’s cheaper and more consistent, because the model draws everything at once with the same palette and lighting.

For a set of different objects it works brilliantly. I asked for 100 fantasy inventory icons and got 156, about 140 of them distinct, for one $0.039 call. That’s about $0.00025 per icon. Ordinary code then slices them by shape, removes the background and repacks them on a clean grid.

156 varied fantasy item icons Raw output, 156 icons in one image The same icons sliced, background removed and repacked Sliced and repacked in code

For one character in many poses it falls apart quickly. A 4 x 4 grid gave me about 13 distinct poses of the same knight. Asking for 10 x 10 gave me the same knight stamped 120 times.

4x4 knight sheet with 16 varied poses 4 x 4, real variety 120 near-identical knights 10 x 10, 120 near-clones

Here is what I learned from seven calls and about $0.27.

  • The sweet spot is about 10 x 10 for sets of objects and 4 x 4 for character poses. Asking for more reduced both variety and quality.
  • The model picks the grid, not you. I asked for 8 x 8 and got 6 x 6. Never slice on a fixed grid. Find sprites by shape.
  • Scale everything together. If each sprite is resized to fill its own cell, a character grows and shrinks between animation frames.

Doing pixel art properly

What I did was a basic experiment with general models. If you need pixel art (a very specific type of image generation), there are better routes.

PixelLab is built for game developers. It draws real sprites at their actual size rather than a large picture that looks like pixel art. Palette, outline, camera angle and transparency are settings rather than prompt wording, and it can animate and rotate a character. It’s a subscription, so I didn’t use it here. Retro Diffusion is a similar commercial option.

To do it yourself, use a model add-on trained for pixel art such as Pixel Art XL, then shrink the result onto a clean grid and fix the palette in code. PixelOE can also turn any image into pixel art without AI. That works well for still images. Consistent animation is where the commercial tools are still ahead.

Where the line falls

Vectors are another format that looks easier than it is. An LLM can write SVG code cheaply, but characters come out crude because it’s describing a picture without seeing it. Dedicated text-to-vector models like Recraft look much better but cost more than raster images.

The general lesson from pixel art is that AI is good at “what should this look like?” and bad at “put exactly this in exactly this cell”. What matters isn’t how simple the output looks. It’s whether the hard part is creative or structural. Use the model for the creative part and code for the structure.

Integrating image generation into a coding project

Put a thin interface in front of every provider

Every API is different. OpenAI has an images endpoint. Gemini returns images inside a chat-style response. OpenRouter has two endpoints depending on the model. Local Flux is a Python library or a command line tool. Hide all of that behind one function so switching provider is a config change.

from dataclasses import dataclass, field
from typing import Protocol

from PIL import Image


@dataclass
class GenerationResult:
    image: Image.Image
    provider: str
    model: str
    prompt: str
    seed: int | None = None
    cost_usd: float | None = None
    metadata: dict = field(default_factory=dict)


class ImageProvider(Protocol):
    def generate(self, prompt: str, *, width: int, height: int, seed: int | None = None) -> GenerationResult: ...

Keep each provider’s quirks inside its own module. Base64 decoding, odd parameter names and fallback endpoints don’t belong in your app code.

Expect the ground to move

These all happened to me within a few months.

  • Models disappeared. The Flux 2 models I planned to use vanished from OpenRouter between planning and building. Imagen 4 was shut down on the Gemini API. Gemini 2.5 Flash Image and gpt-image-1 got retirement dates, some with only weeks of notice. The Gemini deprecations page is worth bookmarking.
  • Endpoints changed. OpenRouter moved dedicated image models to a separate /api/v1/images endpoint. Calling them the old way returns a 404. My code now tries one and falls back to the other.
  • Parameters were ignored or rejected. Gemini ignored size and seed. OpenAI snaps to the nearest of a few fixed sizes. FLUX 3 Klein rejects a seed outright. Muse returned 1600 x 1600 when I asked for 1024 x 1024.

So keep the provider and model in config, never in code. Check the size and format of what came back rather than trusting what you asked for. Run a scheduled smoke test against each provider so you hear about a retirement before your users do.

Log everything

Record the prompt, model, parameters, output size, duration and cost for every image. Cost reporting is inconsistent. OpenRouter returns the billed amount. OpenAI and Gemini return token counts and leave the maths to you. Some return nothing. Without your own logging you only find out what you spent from the monthly bill.

Keeping the prompt and parameters with each image also means you can reproduce or tweak a good result months later.

Treat generation as a slow, failing job

  • It’s slow. Anything from a few seconds to a minute. Don’t do it inside a web request. Put it on a job queue and notify the user when it’s done.
  • It fails. Timeouts, rate limits, content refusals and the odd empty response. Retry with backoff, but treat a refusal as final rather than retrying the same prompt.
  • It costs money per call. Cache results by a hash of the prompt and parameters. Put per-user limits on anything that lets users trigger generation.

Do the exact work in code

Use the model for the creative part only. Background removal (rembg), resizing, palette reduction, cropping, compositing text and packing sprite sheets are all free, fast and deterministic in Python. Most models can’t produce real transparency, so generate on a plain background and remove it. Some of OpenAI’s GPT Image models are an exception with a transparent background option.

If your image needs exact text, like prices or dates, consider generating the artwork without it and adding the text in code. It’s the one way to guarantee it’s spelled right.

Test without spending money

Use a fake provider that returns a fixed image in your unit tests. Keep real API calls for a small set of smoke tests. Use a cheap model for development and switch to the better one in production config.

Integrating with an agent

Image generation and AI agents fit together well. A multimodal agent can write a detailed prompt, call the model, look at the result, critique it and try again. That loop is the part that used to need a human.

Claude doesn’t generate images

If you are using Claude code, you should bear in mind that the built-in model won’t generate raster images like photos or paintings. Anthropic’s help centre says so plainly. Claude can view and analyse images and it can draw diagrams, charts and interactive visuals with code like SVG and HTML.

Its models are aimed at text, code and agents. Claude is very good at the parts around image generation, like writing a detailed prompt and judging whether the result match the brief. You connect it to a specialist image model through a tool and let each do what it’s good at.

Three ways to wire it up

  1. A command line tool. The simplest option. If your agent can run shell commands, give it a script like python -m image_gen generate --model meta/muse-image --prompt "..." and tell it where the output goes. That’s what I did.
  2. An MCP server. The Model Context Protocol lets you expose image generation as a tool to any MCP-compatible agent, such as Claude Code, Claude Desktop or Cursor. The agent sees a typed tool with a description rather than having to learn your script.
  3. Function calling in your own agent. If you’re building the agent yourself, define a generate_image tool in your LLM API call and run the generation when the model asks for it.

A minimal MCP server in Python looks something like this.

from mcp.server.fastmcp import FastMCP, Image

from image_gen.config import get_provider

mcp = FastMCP("image-gen")


@mcp.tool()
def generate_image(prompt: str, width: int = 1024, height: int = 1024) -> Image:
    """Generate an image from a detailed prompt. Costs about $0.01 per call."""
    result = get_provider("openrouter", model="meta/muse-image").generate(prompt, width=width, height=height)
    path = save_with_metadata(result)  # your own logging and storage
    return Image(path=path)


if __name__ == "__main__":
    mcp.run()

Returning the image itself, not just a file path, lets the agent see what it made.

How the experiments in this guide were run

Every image in this guide was made that way, using Claude Code as the agent with a small Python script for calling the providers. I described what I wanted in plain English. Claude wrote the detailed prompts, picked models from OpenRouter’s price list, ran the script, opened each image and told me honestly what was wrong with it. When OpenRouter changed its API mid-session it found the error, fixed the code and carried on.

Tips for agent-driven generation

  • Let the agent write the prompts. LLMs are very good at turning “a 90s burger poster that doesn’t look AI” into a detailed, structured prompt. The poster prompts in this guide were written that way.
  • Make sure it can see the output. The critique step is where the value is. An agent that can’t view the image is just a slow way of calling an API.
  • Put the cost in the tool description. Agents make better decisions when they know a call costs a cent or 15 cents.
  • Set a budget. A loop that keeps trying to improve an image can run up a bill. Cap the number of calls or the spend per session.
  • Ask for honest critique. Ask the agent to list what’s wrong with each image, not just whether it’s done. It was the agent that spotted the 2010s milkshake cup in my 1996 poster.
  • Keep the human on taste. The agent is good at checking an image against a brief. Deciding whether the brief was right in the first place is still your job.

Glossary

See also AI Glossary.

  • Diffusion - Generating an image by starting from random noise and removing it step by step, guided by the prompt.
  • Latent - The compressed internal canvas a diffusion model works on. The reason tiny output sizes don’t work.
  • Steps - How many denoising passes a diffusion model makes. More is slower and usually better, up to a point.
  • Distilled model - A model trained to produce good results in a few steps, like Flux Schnell or SDXL Turbo.
  • Seed - The random starting point. Same seed, prompt and model gives the same image, where supported.
  • Tokeniser - The part of a model that splits text into chunks. The reason older models couldn’t spell.
  • Tokens - The unit a multimodal model counts output in. For images, a fixed number per size and quality.
  • Open weights - A model you can download and run yourself, like Flux or SDXL.
  • LoRA - A small add-on that teaches an open model a new style or subject without retraining it.
  • ControlNet - A way to make a diffusion model follow a sketch, pose or depth map.
  • Inpainting - Regenerating only a masked part of an image.
  • C2PA - A metadata standard that records how an image was made, including whether AI was involved.
  • SynthID - Google’s invisible watermark for AI-generated content.
  • MCP - The Model Context Protocol, a standard way to give AI agents access to tools.
  • Nearest-neighbour - A resize method that copies pixels rather than blending them. Essential for pixel art.
  • Orthographic - A view with no perspective. Useful prompt language for top-down shots.

Resources

See also AI Agents, AI Assisted Coding and Machine Learning Models.