惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
Martin Fowler
Martin Fowler
MongoDB | Blog
MongoDB | Blog
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
aimingoo的专栏
aimingoo的专栏
I
InfoQ
B
Blog
WordPress大学
WordPress大学
Jina AI
Jina AI
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
酷 壳 – CoolShell
酷 壳 – CoolShell
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
Google Developers Blog
C
Check Point Blog
月光博客
月光博客
L
LangChain Blog
GbyAI
GbyAI

V2EX

我用 AI 写代码,但终端管理反而成了累赘——于是我做了 codux [调研] 各位在公司都用什么 ide 和 agent 写代码? 老运维 share 一个运维平台 看到有公司考核 token 指标,很好奇大家上个月的 AI 账单是多少 GLM-Coding 调用持续报错: z.ai 的 Lite 套餐几乎无法使用,官方 Pro/Max 是否稳定? 现在还有什么渠道可以稳定安全地使用 Claude 吗? 上海漕河泾内推,本组有 2 个 hc,一个后端,一个前端,预算都是 20k 左右,不打卡,氛围好 如果 V2EX 上有一组不永久保存聊天记录(比如只保存 7 天或者 24 小时)的聊天室,那么会开启哪些有用或者有趣的可能? gemini cli 貌似挂了,一直返回 403 第一次在自媒体上赚到钱 收集了最近在使用的低价 GPT, Gemini,邮箱等 AI 会员的小店合集 讨论个大实话:现在企业还在说 AI 编程提效 20%, 30%的,真的太落后,没用懂 AI。因为包括很多前沿公司,已经狂奔到提效 200%-500%的情况 [招聘][远程][币安] 前端/后端/QA/iOS/Android 至少 3 年以上经验 目前有大量 HC 欢迎投递 Chatgpt Pro 用量用不完的可以开这些设置 面试的时候好像遇到钓鱼了,给各位避个坑 cursor 年续费 22 号到期, 自动续费是否还是老的计次套餐呢 被两件破事毁掉的一下午,琐碎的内耗消磨人的精力 使用 Planet 存储 Codex 的会话或者重要信息 如果业务部门领导不要你开发功能,而是要求你教会它用 claude code 开发功能,你会怎么做? 分享一个 MacOS 接绿联 CM818 USB 转 DP 转接器使用感受 我的 HR 朋友 10 年老 Java ,非全大专,大家帮忙看看简历 开源了一个 AI 口语练习工具,音素级发音评分,完全免费可自部署 V2EX 上有哪些你觉得很有趣、印象深刻的妹纸? 字节为啥不出个国内版 Vercel? 有在大马的朋友吗? 问个运营商问题 你们在有领导的公司大群发过的最大胆的消息是什么 公司裁员,目前没有工作。想试试摆摊,做一个移动鲜啤打酒车 我的硬盘 Memblaze Pblaze 5 Linux 下不识别,给 Linux 内核提交了补丁, AI 说有望被合并 只有我一个人觉得 codex 不好用?
基于 nano banana2 实现的 AI 图像精确 P 图与蒙板替换技术( ...
lmj243 · 2026-03-28 · via V2EX

基于 Dataler.com API 的 AI 图像精确 P 图实现,使用 MASK 蒙板技术实现像素级精确的产品替换。
项目发布到了 GITHUB,链接:https://github.com/lmj243-code/dataler-ai-image-inpainting
PHP License
平台推荐
Dataler.com - 专业的第三方 AI 中转 API 平台//官方 2.2 折价格,大幅降低 AI 图像生成成本
对接几乎所有主流 AI 图像生成模型
动态负载自适应,智能调度确保稳定高效
兼容 Gemini API 格式,无缝迁移现有项目
API 端点: https://dataler.com/v1beta/models/{model}:generateContent
核心功能
1. AI 反推 Prompt
自动分析图片生成详细的 AI 图像生成提示词
2. 垫图换产品生图( Prompt 替换模式)
分析垫图产品特征
智能融合 Prompt
生成新图
3. 原图产品换垫图产品(双图融合模式)
反推场景图(人物、光线、氛围)
反推产品图(外观、材质、颜色)
智能整合生成
4. 精确 P 图 - MASK 蒙板替换模式 ⭐
这是最精确的图像替换技术
技术原理与流程
MASK 蒙板替换工作流程
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ 垫图(场景图) │ │ 产品图(新) │ │ 用户描述(可选) │
└────────┬────────┘ └────────┬────────┘ └────────┬────────┘
│ │ │
▼ │ │
┌─────────────────┐ │ │
│ AI 生成 MASK 蒙板 │◄─────────────┴───────────────────────┘
│ 白色=替换区域 │ (根据用户描述或自动识别主体)
│ 黑色=保留区域 │
└────────┬────────┘


┌─────────────────────────────────────────────────────────────┐
│ Inpainting 生成阶段 │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ 原图场景 │ │ MASK 蒙板 │ │ 新产品图 │ │
│ │ (保留) │ │ (黑白) │ │ (替换来源) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │ │ │
│ └──────────────┼──────────────┘ │
│ ▼ │
│ ┌─────────────────┐ │
│ │ AI Inpainting │ │
│ │ 精确替换生成 │ │
│ └─────────────────┘ │
└─────────────────────────────────────────────────────────────┘
详细步骤
步骤 1:图片准备
读取垫图(场景图)和新产品图
智能压缩(最大边 1500px ,保持质量同时减少传输)
转换为 Base64 格式
步骤 2:AI 生成 MASK 蒙板
根据用户是否提供目标描述,采用不同策略:
策略 A - 用户指定替换目标:
Prompt 示例:
"Generate an image: 请仔细观察这张图片,为 inpainting 创建一张精确的黑白蒙版(MASK)图。
[要做成白色蒙版的目标区域]
[用户描述的目标物品]
请把上述描述的所有内容(包括它们占据的完整区域)都涂成纯白色(#FFFFFF)。
图片中其他所有内容(背景、墙壁、地板、人物、文字、其他不相关的物品)都涂成纯黑色(#000000)。
Rules:
- The MASK must be the EXACT SAME dimensions as the original image
- WHITE (#FFFFFF) = the target area described above (to be replaced)
- BLACK (#000000) = everything else (to be kept)
- Cover the ENTIRE target area including all parts mentioned in the description
- Use smooth edges with a small margin (3-5 pixels) around the target
- Clean black and white only, NO gray, NO gradients
- Do NOT include shadows or reflections in the white area
- Output ONLY the mask image, no text."
策略 B - 自动识别主体:
Prompt 示例:
"Generate an image: Look at this image carefully. Create a precise MASK image for inpainting.
The MASK must be the EXACT SAME dimensions as the original image.
Identify the MAIN PRODUCT/SUBJECT in the image and mask it.
Rules:
- Paint the MAIN PRODUCT/SUBJECT area in PURE WHITE (#FFFFFF)
- Paint EVERYTHING ELSE in PURE BLACK (#000000)
- Cover the product outline with a small margin (3-5 pixels)
- Use smooth edges, no jagged borders
- Clean black and white only, NO gray, NO gradients
- Do NOT include shadows in the white area
- Output ONLY the mask image, no text."
步骤 3:反推产品外观特征
AI 分析新产品图,提取:
整体形状和轮廓
尺寸比例
精确颜色(深空灰、象牙白、玫瑰金等)
材质质感(金属/塑料/木材/玻璃/布料,哑光/亮面/磨砂)
表面细节(纹理、图案、反光特性、logo 位置)
结构特征(按钮、接口、把手、铰链、缝线)
产品数量和排列
步骤 4:Inpainting 精确替换
构建包含四部分的请求:
文本指令:详细的替换规则说明
原图场景:作为背景保留
MASK 蒙板:黑白图,白色区域将被替换
新产品图:替换来源
关键指令模板:
"Generate an image: I am providing three images:
1. The FIRST image is the original photo (the scene/background to keep)
2. The SECOND image is a black-and-white MASK where WHITE areas indicate the region to replace
3. The THIRD image is the new product/object that should be placed into the white masked area
**[PRODUCT APPEARANCE REFERENCE - from the THIRD image]**
[反推的产品外观描述]
**[CRITICAL - PRODUCT FIDELITY RULES]**
The product from the THIRD image must be reproduced with 100% visual fidelity:
- EXACT original shape, proportions, and aspect ratio — NO stretching, squishing, warping, or distortion
- EXACT original colors, materials, textures, surface details, logos, and text
- EXACT original structural features (buttons, handles, edges, curves, patterns)
- Scale the product uniformly to fit the masked area — maintain width-to-height ratio strictly
- If the masked area is a different shape than the product, fit the product within the area with appropriate background fill — do NOT deform the product to fill the mask
- The product in the result must look like an exact copy of the THIRD image, just placed into a new scene
Placement rules:
- Adjust ONLY the viewing angle slightly to match the scene perspective
- Match the scene lighting direction and color temperature on the product surface
- Add natural shadows consistent with the scene light source
- Blend edges seamlessly with the surrounding area
- Keep ALL black masked areas (background, people, environment) EXACTLY unchanged
- Preserve the exact resolution and aspect ratio of the original image"
技术要点
蒙板生成要点
纯黑白:不允许灰色或渐变
边缘平滑:3-5 像素的过渡边距
不包含阴影:白色区域仅包含产品本身
尺寸一致:MASK 必须与原图尺寸完全相同
产品保真要点
形状不变:禁止拉伸、压缩、变形
比例保持:宽高比严格保持
材质还原:颜色、纹理、反光特性 100%还原
结构完整:所有可见部件必须保留
场景融合要点
透视匹配:根据场景调整产品视角
光影一致:匹配场景光源方向和色温
阴影自然:添加符合光源的阴影
边缘融合:与周围环境无缝衔接
应用场景
电商产品替换:模特手持产品图,快速替换不同款式
场景营销图:保持精美场景,更换展示产品
广告素材制作:批量生成同一产品的不同场景展示
产品迭代展示:同一角度展示产品不同配色/配置
虚拟试穿/试用:将产品自然融入用户场景
快速开始
环境要求
PHP 8.0+
cURL 扩展
GD 扩展(用于图片处理)
安装
git clone https://github.com/lmj243-code/dataler-ai-image-inpainting.git
cd dataler-ai-image-inpainting
使用方法
1. 基本用法(自动识别主体)
require_once 'DatalerInpaintingAPI.php';
$apiKey = 'your-api-key-here';
$api = new DatalerInpaintingAPI($apiKey);
$result = $api->replaceProductWithMask(
'scene.jpg', // 场景图:模特手持旧产品
'new_product.jpg', // 新产品图:要替换进去的产品
null, // 不指定目标,自动识别
'output.png', // 输出路径
true // 启用压缩
);
2. 指定替换目标
$result = $api->replaceProductWithMask(
'model_with_bag.jpg', // 场景图:模特拿着红色手提包
'blue_bag.jpg', // 新产品图:蓝色手提包
'红色手提包', // 明确指定要替换的是红色手提包
'output_blue_bag.png', // 输出路径
true
);
3. 命令行运行示例
# 运行示例 1:自动识别
php DatalerInpaintingAPI.php 1
# 运行示例 2:指定替换目标
php DatalerInpaintingAPI.php 2
# 运行示例 3:分步调用
php DatalerInpaintingAPI.php 3
# 运行示例 4:批量处理
php DatalerInpaintingAPI.php 4
# 运行示例 5:自定义日志
php DatalerInpaintingAPI.php 5
API 参考
DatalerInpaintingAPI 类
构造函数
public function __construct(string $apiKey)
主要方法
方法 说明
replaceProductWithMask() 一键完成完整的蒙板替换流程
generateMask() AI 生成 MASK 蒙板
analyzeProduct() 反推产品外观特征
inpaint() 执行 Inpainting 精确替换
compressImage() 智能图片压缩
imageToBase64() 图片转 Base64
支持的模型
gemini-3-pro-image-preview:专业图像生成模型
gemini-3.1-flash-image-preview:快速图像生成模型
最佳实践
图片质量:建议使用清晰、光线均匀的产品图
描述精确:用户提供的目标描述越详细,MASK 定位越准确
多试几次:AI 生成有一定随机性,不满意可多次尝试
尺寸匹配:场景图和产品图分辨率建议相近
压缩策略:大图片适当压缩可提升 API 响应速度
文件说明
文件 说明
DatalerInpaintingAPI.php 核心 API 类,包含完整实现
GUIDE.md 详细技术文档和原理说明
README.md 项目说明文档
作者
LT - 初始代码和文档
许可证
本项目采用 MIT 许可证 - 详见 LICENSE 文件
致谢
Dataler.com - 提供稳定高效的 AI API 服务
Gemini - 强大的图像生成模型