图片水印裁剪缩略接口实战:水印 position 九宫格与 xy 偏移怎么设
# 图片水印裁剪缩略接口实战:水印 position 九宫格与 xy 偏移怎么设
> 接口/接入点:图片水印裁剪缩略接口(apiCode=1)· 1-2 添加图片水印 · 是否免费:免费 · 请求方式:POST/GET · 返回格式:JSON · 适用人群:前端、全栈 · 阅读时间:6 分钟
## 核心要点
- 水印位置用 `position` 九宫格控制,默认右下角;`xy` 做像素级微调(`+5+10` = 右移 5、下移 10)。
- 1-2 需要传两张图:`src_img`(底图)、`logo_img`(水印图),**均必填**。
- 想要不落盘传图,可用 1-3 的 base64 版(见[base64 选型篇](https://www.showapi.com/guides/base64-vs-file-1))。
## Why:水印位置为什么重要
版权水印放角落不挡主体,放居中防盗但碍观感。用 `position` 选定九宫格区域、再 `xy` 微调几像素,就能在"够显眼"和"不碍事"之间取到平衡。
## What:参数速览
| 参数 | 必填 | 说明 |
|------|------|------|
| `src_img` | 是 | 底图文件(multipart) |
| `logo_img` | 是 | 水印图文件(multipart) |
| `position` | 否 | NorthWest/North/NorthEast/West/Center/East/SouthWest/South/SouthEast,**默认右下角** |
| `xy` | 否 | 如 `+5+10`,水印下边缘距原图 10px、右边缘距原图 5px |
九宫格位置示意:
```
NorthWest ┆ North ┆ NorthEast
──────────┼────────┼──────────
West ┆ Center ┆ East
──────────┼────────┼──────────
SouthWest ┆ South ┆ SouthEast(默认)
```
## How:放置右下角并微调
**Python(requests)**
```python
import requests
url = "https://route.showapi.com/1-2"
data = {"position": "SouthEast", "xy": "+5+10"}
files = {"src_img": open("base.jpg", "rb"), "logo_img": open("logo.png", "rb")}
r = requests.post(url, params={"appKey": "YOUR_APPKEY"}, data=data, files=files, timeout=10)
body = r.json()["showapi_res_body"]
if str(body.get("ret_code")) == "0":
print("合成图:", body["des_pic_url"])
```
**cURL**
```bash
curl -X POST "https://route.showapi.com/1-2?appKey=YOUR_APPKEY" \
-F "position=SouthEast" -F "xy=+5+10" \
-F "src_img=@base.jpg" -F "logo_img=@logo.png"
```
**Node.js(fetch)**
```javascript
import fs from "fs";
const form = new FormData();
form.append("position", "SouthEast"); form.append("xy", "+5+10");
form.append("src_img", new Blob([fs.readFileSync("base.jpg")]), "base.jpg");
form.append("logo_img", new Blob([fs.readFileSync("logo.png")]), "logo.png");
const b = (await (await fetch("https://route.showapi.com/1-2?appKey=YOUR_APPKEY",{method:"POST",body:form})).json()).showapi_res_body;
if (String(b.ret_code) === "0") console.log("合成图:", b.des_pic_url);
```
## 返回示例
```json
{ "showapi_res_body": { "ret_code": "0", "des_pic_url": "http://img.showapi.com/1.jpg" } }
```
## 进阶/边界
- 不传 `position` 时水印落在右下角;想放正中央用 `Center`。
- `xy` 偏移是相对水印自身边缘到原图边缘的距离,正值向原图内侧缩。
- 水印图建议用透明背景 PNG,叠加更自然。
## FAQ
**Q1:只传底图报错?** 1-2 要求 `src_img` 与 `logo_img` 都传,缺一个会失败。
**Q2:xy 怎么写?** 形如 `+5+10`(右 5、下 10);负值向反方向,按文档示例格式传。
**Q3:想批量打水印?** 循环调用并注意限流,见[电商方案](https://www.showapi.com/guides/ecommerce-image-plan-1)与[频率控制篇](https://www.showapi.com/guides/image-process-ratelimit-1)。
**Q4:base64 入参版?** 用 1-3 接入点,参数同名,仅把文件换成 `src_img_base64`/`logo_img_base64`。
## 相关能力 / 下一步阅读
- [图片水印裁剪缩略接口:base64 与文件上传两种入参怎么选](https://www.showapi.com/guides/base64-vs-file-1)
- [图片水印裁剪缩略接口:电商商品图批量水印+缩略一体化方案](https://www.showapi.com/guides/ecommerce-image-plan-1)
- **本系列共 13 篇**:查看[图片水印裁剪缩略接口指南总目录](https://www.showapi.com/guides/image-process-guides-1)