跳到正文

图像处理

缩放、压缩、转换、加水印与读取图像元数据。

概览

基础路径: /v1/image
Python 命名空间: infra.image_process
text
# Python
from infrai import infra

# TypeScript
import { InfraiClient } from "@infrai/sdk";
const client = new InfraiClient({ apiKey: process.env.INFRAI_API_KEY! });
// → client.image

方法

image.process

POST /v1/image/process

处理图像:缩放、压缩、转换、加水印、旋转或去背景。

Python

python
infra.image_process.process(...)  # resize / compress / convert / watermark / rotate / removebg

TypeScript

typescript
client.image.resize(opts) | .compress(opts) | .convert(opts) | .watermark(opts) | .rotate(opts) | .removebg(opts)

参数

名称类型必填说明
inputImageRef { url? | base64? }
必填
图像引用,URL 或 base64。
widthnumber可选目标宽度(像素)。
heightnumber可选目标高度(像素)。
format"jpeg" | "png" | "webp" | "avif"可选输出图像格式。
qualitynumber可选压缩质量,0 到 100。
idempotency_keystring可选可选去重 key;相同重试将返回同一结果。

返回

ImageOpResult { image_url, mime, width?, height?, bytes }

示例

一次性前置(每个范例都假定已完成):

bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...
python
from infrai import infra

out = infra.image_process.resize(input={"url": "https://ex.com/photo.jpg"}, width=800)
print("image url:", out.get("image_url"))

image.metadata

POST /v1/image/metadata

读取图像尺寸、MIME 类型与 EXIF 元数据。

Python

python
infra.image_process.metadata(input=...)

TypeScript

typescript
client.image.metadata(opts: { input }): Promise<{ width, height, mime, exif? }>

参数

名称类型必填说明
inputImageRef { url? | base64? }
必填
图像引用,URL 或 base64。

返回

{ width, height, mime, exif? }

示例

一次性前置(每个范例都假定已完成):

bash
pip install infrai
# one-time auth (no secret needed): anonymous account + trial, writes ~/.infrai/credentials
python -c "from infrai import infra; infra.activate()"
# returning user instead: export INFRAI_API_KEY=ifr_pk_proj_...
python
from infrai import infra

meta = infra.image_process.metadata(input={"url": "https://ex.com/photo.jpg"})
print(meta.get("width"), "x", meta.get("height"), meta.get("mime"))