Web编程技术营地
研究、演示、创新

pollinations.ai

撰写时间:2025-05-20

修订时间:2025-02-20

概述

Pollinations.ai是国外的一个免费的文生图的AI工具,支持HTML网址的参数调用。

基本使用方法

const { NumUtils } = await import('/js/esm/NumUtils.js'); let promptURL = `https://image.pollinations.ai/prompt/`; let prompt = "A chinese ancient soldier riding a white horse, with a long sword in hand, is fighting with mass enemies. The battle field is covered with bloods of enemies."; let width = 1024; let height = width * 3 / 4; let seed = NumUtils.GetRandomIntInclusive(1, 300); let model = 'flux'; let nologo = true; let src = `${promptURL}${encodeURIComponent(prompt)}?width=${width}&height=${height}&seed=${seed}&model=${model}&nologo=${nologo}`; pc.log(src); pc.log(seed); let img = document.querySelector('img'); img.src = src;
img { height: 300px; aspect-ratio: 4 / 3; }

出图的格式为:

https://image.pollinations.ai/prompt/{prompt}?width={width}&height={height}&seed={seed}&model={model}&nologo=true

API调用接口格式由prompt, width, height, seed, model, nologo等构成。

其中,prompt是提示词,需使用英文来描述。也可使用中文,但有时中文效果不佳。

上面的代码每次运行,都会自动生成一个不同的seed,从而生成不同的图像。当我们觉得图像效果较好时,可固定使用该seed的值来生成同一图像。例如,当seed35, 134, 179296时,效果比较不错。

让DeepSeek生成提示词

Pollinations.ai的提示词相对比较复杂,此时可让DeepSeek来帮我们生成精准的提示词。

首先,向DeepSeek发出以下指令:

你现在是一个根据文本来生成图像的提示词生成器。 我将向你描述一张图像,你充分展开你的想像力,按下面的格式来生成一个可生成图像的提示词。 {sceneDetailed}%20{adjective}%20{charactersDetailed}%20{visualStyle}%20{genre}%20{artistReference} 生成提示词后,使用下面的HTML来显示图像。 <img src=`https://image.pollinations.ai/prompt/{description}?width={width}&height={height}&nologo=true` /> 上面的{description}即是你所生成的提示词。提示词需要进行URL编码。

DeepSeek提示收到。此时,DeepSeek已经变身为一个资深的艺术家。接着,我们向DeepSeek发出以下指令:

为唐诗《静夜思》生成中国古风的图像。画面中要有床前明亮的月光,好像地上的霜。要体现出诗人低头思故乡的感情。

DeepSeek帮我们生成如下图像:

MSG
静夜思

DeepSeek根据我们的要求,使用英语按格式帮我们生成了提示词,并以此提示词创建了一个URL;同时创建了一个img标签,并将该标签的src属性值设置为该URL。这样,当我们在HTML页面中使用了该img时,将向Pollinations.ai发起一个请求,Pollinations.ai即时创建了图像并向我们返回。

上面也可看出,Pollinations.ai支持资源的外链,特别方便;并且国内访问速度也挺快。当然,如果我们对图像效果满意,则可下载为本地资源来使用。

参考资源

  1. pollinations.ai
  2. pollinations.ai in Github
  3. PollinationsAI Enhancer in Github