GPT Image 2 批量生图实战:网页验证与无限并发异步 API
本文给出一套适合跨境电商多 SKU 的图片生产流程先在网页“自由绘图”验证商品一致性提示词再通过POST /v1/images/generations异步提交批量任务以GET /v1/tasks/{task_id}查询状态并下载result_urls。同一方法也可用于短剧、广告、游戏、文旅、出版和 POD 等稳定的批量生图任务。体验入口https://www.maizimarket.com/1. 先在网页验证输入和提示词真实导航为“创意中心—快速创作—自由绘图”。已有商品原图时选择“批量图生图”。上传清晰、完整的商品图后使用下面的商品一致性提示词Use the uploaded product image as the only source of truth. Create a premium lifestyle product photograph for a cross-border ecommerce listing. Keep the exact product shape, proportions, colors, materials, buttons, logo and packaging details. Use realistic commercial lighting, physically accurate reflections and shadows, a clean square composition, and clear product focus. Do not add or remove product parts. Do not invent accessories, functions, certifications or claims. No promotional text, price, discount badge, watermark, extra logo or duplicated product.模型选择 GPT Image 2商品主图先测试 1:1、1K、1 张。点击“立即生成”完成后核对形状、颜色、材质、Logo、包装文字、按钮、配件和商品数量。通过就下载不通过则调整提示词后重新生成。下面是站内帮助区的内置商品输入与场景结果示例并非本文新执行的生成批次。2. API 契约基础地址https://www.maizitech.xyz动作接口关键结果提交图片任务POST /v1/images/generationsdata[0].task_id查询任务GET /v1/tasks/{task_id}status、result_urls、error_msg认证头为Authorization: Bearer sk-...。Key 必须放在环境变量或密钥管理服务不要硬编码。任务状态包括queued、pending、processing、completed、failed和violation。建议图片任务每 5–10 秒查询一次完成 URL 只保留有限时间应及时下载到自己的对象存储。3. 可运行的 asyncio httpx 示例先安装依赖并设置 KeypipinstallhttpxexportMAIZIAI_API_KEYsk-...importasyncioimportosfrompathlibimportPathimporthttpx BASE_URLhttps://www.maizitech.xyzAPI_KEYos.environ[MAIZIAI_API_KEY]TERMINAL{completed,failed,violation}PROMPTUse the uploaded product image as the only source of truth.\n\nCreate a premium lifestyle product photograph for a cross-border ecommerce listing.\n\nKeep the exact product shape, proportions, colors, materials, buttons, logo and packaging details.\nUse realistic commercial lighting, physically accurate reflections and shadows, a clean square composition, and clear product focus.\n\nDo not add or remove product parts.\nDo not invent accessories, functions, certifications or claims.\nNo promotional text, price, discount badge, watermark, extra logo or duplicated product.asyncdefsubmit(client,item):responseawaitclient.post(/v1/images/generations,json{model:gpt-image-2,prompt:PROMPT,images:[item[image_url]],size:1:1,image_size:1K,n:1,},)response.raise_for_status()returnresponse.json()[data][0][task_id]asyncdefwait_result(client,task_id,timeout_seconds600):deadlineasyncio.get_running_loop().time()timeout_secondswhileasyncio.get_running_loop().time()deadline:responseawaitclient.get(f/v1/tasks/{task_id})response.raise_for_status()taskresponse.json()iftask[status]inTERMINAL:iftask[status]!completed:raiseRuntimeError(task.get(error_msg)ortask[status])returntask[result_urls]awaitasyncio.sleep(5)raiseTimeoutError(ftask{task_id}timed out)asyncdefgenerate_one(client,semaphore,item):asyncwithsemaphore:task_idawaitsubmit(client,item)urlsawaitwait_result(client,task_id)outputPath(downloads)/f{item[sku]}.pngoutput.parent.mkdir(exist_okTrue)imageawaitclient.get(urls[0])image.raise_for_status()output.write_bytes(image.content)returnitem[sku],task_id,outputasyncdefmain():items[{sku:SKU-001,image_url:https://example.com/sku-001.png},{sku:SKU-002,image_url:https://example.com/sku-002.png},]headers{Authorization:fBearer{API_KEY}}timeouthttpx.Timeout(30,read60)semaphoreasyncio.Semaphore(5)asyncwithhttpx.AsyncClient(base_urlBASE_URL,headersheaders,timeouttimeout,follow_redirectsTrue)asclient:resultsawaitasyncio.gather(*(generate_one(client,semaphore,item)foriteminitems),return_exceptionsTrue,)forresultinresults:print(result)asyncio.run(main())接口支持无限并发调用另一台服务器的批量测试成功率为 99%。示例为了控制单个客户端资源占用将本地并发设为 5单请求与总轮询都有超时失败/违规会保留为异常完成后从result_urls下载。正式系统还应将 SKU、task_id、状态、重试次数和目标文件地址持久化。4. 行业与任务映射行业输入输出建议工作流跨境电商SKU 原图、包装、品牌规范主图、场景图、A 图、尺寸变体网页验一款API 批量 SKU短剧/网文角色参考、剧本、场景设定海报、定妆图、分镜气氛图网页锁角色API 按集生产广告营销品牌资产、产品图、BriefKV、广告变体、社媒素材网页确认创意API 跑渠道版游戏角色、道具、世界观设定概念图、道具图、场景图网页迭代核心设定API 补资产建筑/室内模型截图、线稿、材质参考外立面、室内、夜景表达网页逐张结构校核文旅景点照片、文化元素、路线海报、路线视觉、文创方向网页定方向API 多尺寸输出内容出版文章、脚本、栏目模板封面、插图、轮播、缩略图模板稳定后用 APIPOD/服装版型、印花主题、尺寸花型、印花图、展示图网页查版式版权API 批量5. 结果检查与重试边界电商图片应核对商品事实、品牌文字和平台规范短剧与游戏应核对角色和道具一致性建筑应核对体量与结构广告、文旅和 POD 还要核对商标、版权、文化表达与可印刷性。网络错误、HTTP 429/5xx 可以有限重试failed和violation应先读error_msg修改输入后再创建新任务。不要因任务处于queued就重复提交否则会产生重复订单。