GPT Image इमेज एडिटिंग API
BetterToken के OpenAI-संगत API से GPT Image संदर्भ संपादित करें। multipart, मास्क, परिणाम सहेजने, बिलिंग और त्रुटियों के उदाहरण देखें।
POST /v1/images/edits
image-to-image endpoint multipart/form-data request body उपयोग करता है। एक या अधिक reference image अपलोड करें और prompt व parameter फ़ील्ड को form field के रूप में भेजें।
अनुशंसित मान
इन्हें शुरुआती मान समझें, हर अनुरोध की अनिवार्य शर्त नहीं। n=1 से शुरू करें; endpoint 1 से 10 तक स्वीकार करता है, लेकिन कम images दे सकता है।
model=gpt-image-2n=1response_format=b64_jsonoutput_format=pngएक reference image के लिए image उपयोग करें। कई reference image के लिए image[] फ़ील्ड दोहराएं। सामान्य reference edit के लिए mask जरूरी नहीं है। quality वैकल्पिक है: gpt-image-2 के लिए auto, low, medium, high; दोनों 2.5 models xhigh और max भी स्वीकारते हैं। multipart में दिए जाने पर यह non-empty string होना चाहिए। स्वीकार हुआ मान model-size-edit संयोजन की availability या बेहतर image quality की गारंटी नहीं देता; नहीं देना हो तो field छोड़ दें। size भी वैकल्पिक है, पर खाली string अमान्य है।
एक संदर्भ चित्र
वास्तविक reference.png तैयार करें और YOUR_API_KEY की जगह अपना BetterToken API Key दें। एक Key सभी उपलब्ध models पर काम करती है। curl -F या Python files को boundary वाला Content-Type बनाने दें।
| Form field | उदाहरण |
|---|---|
model | gpt-image-2 |
prompt | Using this reference image, create a more polished square product key visual while preserving the subject style. |
n | 1 |
size | 1024x1024 |
response_format | b64_json |
output_format | png |
image | स्थानीय file reference.png चुनें |
curl --max-time 300 'https://bettertoken.ai/v1/images/edits' \ -H 'Authorization: Bearer YOUR_API_KEY' \ -F 'model=gpt-image-2' \ -F 'prompt=Using this reference image, create a more polished square product key visual while preserving the subject style.' \ -F 'n=1' \ -F 'size=1024x1024' \ -F 'response_format=b64_json' \ -F 'output_format=png' \ -F 'image=@reference.png'कई संदर्भ चित्र
ये form fields भेजें:
| Form field | उदाहरण |
|---|---|
model | gpt-image-2 |
prompt | Combine these reference images into a promotional poster with a consistent style. |
n | 1 |
size | 1024x1024 |
response_format | b64_json |
output_format | png |
image[] | स्थानीय file reference-1.png चुनें |
image[] | स्थानीय file reference-2.jpg चुनें |
image[] | स्थानीय file reference-3.png चुनें |
curl --max-time 300 'https://bettertoken.ai/v1/images/edits' \ -H 'Authorization: Bearer YOUR_API_KEY' \ -F 'model=gpt-image-2' \ -F 'prompt=Combine these reference images into a promotional poster with a consistent style.' \ -F 'n=1' \ -F 'size=1024x1024' \ -F 'response_format=b64_json' \ -F 'output_format=png' \ -F 'image[]=@reference-1.png' \ -F 'image[]=@reference-2.jpg' \ -F 'image[]=@reference-3.png'मास्क से चुना क्षेत्र संपादित करें
किसी चुने हुए क्षेत्र में edit को निर्देशित करने के लिए वैकल्पिक mask file field जोड़ें। मौजूदा BetterToken endpoint एक mask file पढ़ता है। बार-बार mask[] fields न भेजें।
- मूल image और mask का file format और pixel dimensions एक समान होना चाहिए। हर file 50 MB से छोटी होनी चाहिए। अधिकतम compatibility के लिए दोनों files में Alpha channel वाला PNG इस्तेमाल करें।
- Mask में Alpha channel होना चाहिए। पूरी तरह transparent pixels (
Alpha 0) उस क्षेत्र को दिखाते हैं जिसे model दोबारा बना सकता है। पूरी तरह opaque pixels (Alpha 255) उस क्षेत्र को सुरक्षित रखने के लिए model को निर्देशित करते हैं। - सामान्य RGB black-and-white image तब तक मान्य mask नहीं है जब तक उसमें वास्तव में Alpha channel न हो।
- अगर आप कई
image[]fields भेजते हैं, तो mask पहली input image पर लागू होता है। - Mask model को निर्देशित करता है। यह guarantee नहीं करता कि transparent क्षेत्र के बाहर के pixels बिल्कुल समान रहेंगे।
- Mask की उपलब्धता model और size के संयोजन पर निर्भर करती है। GPT Image 2 और Flare परीक्षणों में target क्षेत्र के बाहर भी स्पष्ट बदलाव दिखे; Sunburst का local-region control सत्यापित नहीं है। Product image, logo या सटीक retouching से पहले परिणाम जांचें।
curl --max-time 300 'https://bettertoken.ai/v1/images/edits' \ -H 'Authorization: Bearer YOUR_API_KEY' \ -F 'model=gpt-image-2' \ -F 'prompt=Replace only the masked area with a blue circle. Preserve everything else.' \ -F 'n=1' \ -F 'size=1024x1024' \ -F 'response_format=b64_json' \ -F 'output_format=png' \ -F 'image=@original.png' \ -F 'mask=@mask.png'Python से मास्क संपादन
दोनों files को एक ही with statement में खोलें, ताकि request के बाद Python उन्हें बंद कर दे:
import requests
url = "https://bettertoken.ai/v1/images/edits"headers = {"Authorization": "Bearer YOUR_API_KEY"}data = { "model": "gpt-image-2", "prompt": "Replace only the masked area with a blue circle. Preserve everything else.", "n": "1", "size": "1024x1024", "response_format": "b64_json", "output_format": "png",}
with open("original.png", "rb") as image_file, open("mask.png", "rb") as mask_file: files = { "image": ("original.png", image_file, "image/png"), "mask": ("mask.png", mask_file, "image/png"), } response = requests.post(url, headers=headers, data=data, files=files, timeout=300)
response.raise_for_status()result = response.json()अपलोड से पहले फ़ाइलें जाँचें
यह छोटा Pillow check अनिवार्य format, dimensions और Alpha channel की जांच करता है। सामान्य local mask के लिए, Alpha channel में पूरी तरह transparent और opaque दोनों areas न होने पर यह warning भी देता है:
from pathlib import Pathfrom PIL import Image
image_path = Path("original.png")mask_path = Path("mask.png")
if image_path.stat().st_size >= 50 * 1024 * 1024 or mask_path.stat().st_size >= 50 * 1024 * 1024: raise ValueError("Each file must be smaller than 50 MB")
with Image.open(image_path) as image, Image.open(mask_path) as mask: if image.format != mask.format: raise ValueError("The image and mask formats must match") if image.size != mask.size: raise ValueError("The image and mask dimensions must match") if "A" not in mask.getbands(): raise ValueError("The mask must contain an Alpha channel") alpha_min, alpha_max = mask.getchannel("A").getextrema() if alpha_min > 0 or alpha_max < 255: print("Warning: for a typical local mask, include fully transparent and fully opaque areas")Python उदाहरण
import base64from pathlib import Path
import requests
url = "https://bettertoken.ai/v1/images/edits"headers = { "Authorization": "Bearer YOUR_API_KEY",}
data = { "model": "gpt-image-2", "prompt": "Using this reference image, create a more polished square product key visual while preserving the subject style.", "n": "1", "size": "1024x1024", "response_format": "b64_json", "output_format": "png",}
with open("reference.png", "rb") as image_file: files = { "image": ("reference.png", image_file, "image/png"), } response = requests.post(url, headers=headers, data=data, files=files, timeout=300)
response.raise_for_status()result = response.json()for index, item in enumerate(result["data"], start=1): encoded = item["b64_json"] if "," in encoded and "base64" in encoded.split(",", 1)[0]: encoded = encoded.split(",", 1)[1] image_bytes = base64.b64decode(encoded) if image_bytes.startswith(b"\x89PNG\r\n\x1a\n"): extension = "png" elif image_bytes.startswith(b"\xff\xd8\xff"): extension = "jpg" elif image_bytes.startswith(b"RIFF") and image_bytes[8:12] == b"WEBP": extension = "webp" else: raise ValueError("Unsupported image signature") Path(f"output-{index}.{extension}").write_bytes(image_bytes)अनुशंसित आकार
size | अनुपात | उपयोग |
|---|---|---|
auto | Auto | अपने-आप आकार चयन |
1024x1024 | 1:1 | वर्गाकार इमेज, avatar, cover, asset |
1536x1024 | 3:2 | landscape poster, banner, scene |
1024x1536 | 2:3 | portrait mobile cover और poster |
1536x1152 | 4:3 | मानक landscape इमेज, product image, content graphic |
1152x1536 | 3:4 | मानक portrait इमेज, mobile cover, vertical poster |
2048x2048 | 1:1 | high-resolution वर्गाकार इमेज |
2048x1152 | 16:9 | high-resolution landscape इमेज |
3840x2160 | 16:9 | 4K landscape इमेज |
2160x3840 | 9:16 | 4K portrait इमेज |
तालिका में दिए मान उदाहरण हैं, पूरी सूची नहीं। size छोड़ सकते हैं या auto दे सकते हैं; JSON null भी automatic selection करता है। स्पष्ट width और height positive, 16 के multiples और अधिकतम 3840 होने चाहिए; aspect ratio 3:1 से अधिक न हो और कुल pixels 655360 से 8294400 तक हों। 2080x2080 भी वैध है। वास्तविक dimensions अलग हो सकती हैं; image जांचें और जबरन crop न करें।
इमेज सहेजें
सफल response OpenAI-compatible इमेज response स्वरूप का पालन करता है। नीचे की रकम receipt format का उदाहरण हैं, fixed prices नहीं:
{ "created": 1710000000, "data": [ { "b64_json": "iVBORw0KGgoAAAANSUhEUgAA...(truncated)", "revised_prompt": "Optional revised prompt" } ], "bettertoken_image_billing": { "currency": "CNY", "settled_amount": "0.15", "outputs": [ { "index": 0, "width": 1254, "height": 1254, "billing_resolution": "2K", "currency": "CNY", "amount": "0.15" } ] }}हर data[].b64_json image पढ़ें और revised_prompt जैसे अतिरिक्त fields स्वीकार करें। output_format format request है, encoding या conversion की guarantee नहीं। bytes जांचें और सही extension से save करें। Text to image में हर output के PNG, JPEG या WebP signature को पहचानने वाले examples हैं।
वास्तविक लागत देखें
bettertoken_image_billing परिणाम के साथ लौटता है; अलग cost request की जरूरत नहीं है।
| Field | अर्थ |
|---|---|
currency | Settlement currency; यह हमेशा USD या CNY नहीं होती। |
settled_amount | इस request की कुल settled रकम, decimal string। |
requested_resolution | Request से पहचाना गया resolution tier। |
outputs[].index | data[] से जुड़ा zero-based image index। |
outputs[].width, height | Image की वास्तविक चौड़ाई और ऊंचाई, pixels में। |
outputs[].billing_resolution | Image का अंतिम billing tier। |
outputs[].currency, amount | Image की settlement currency और रकम। |
अनुरोधित आकार और billing tier अलग हो सकते हैं: 1024x1024 request से 1254x1254 image मिली है और 2K billing हुई है। Receipt की वास्तविक रकम लें और वर्तमान कीमत model plaza में देखें। दिखाते समय रकम string रखें, गणना में decimal arithmetic करें। Receipt न होना शून्य लागत नहीं है।
from decimal import Decimal
billing = result.get("bettertoken_image_billing")if billing is None: print("Billing receipt unavailable; check your usage records.")else: total = Decimal(billing["settled_amount"]) print(f"Total: {billing['currency']} {total}") for output in billing.get("outputs", []): print( f"Image {output['index'] + 1}: " f"{output['width']}x{output['height']}, " f"{output['billing_resolution']}, " f"{output['currency']} {output['amount']}" )प्रतिक्रिया प्रवाह
यह endpoint synchronous है। POST /images/edits भेजने के बाद server के response तक मौजूदा HTTP request खुला रखें। सफलता पर सभी images data[].b64_json में लौटती हैं।
endpoint task_id नहीं लौटाता, और कोई अलग status query या result download endpoint नहीं है।
समय सीमा, दोबारा अनुरोध और बिलिंग
HTTP client का timeout कई मिनट रखें। Timeout, disconnect या अस्पष्ट 5xx से यह पता नहीं चलता कि edit पूरा हुआ या charge लगा; result लिखते समय connection टूट सकता है। POST को अपने-आप दोबारा न भेजें। पहले usage records या request logs जांचें और मिला हुआ request ID तथा X-BetterToken-Image-Receipt और X-BetterToken-Billing-State headers सुरक्षित रखें। Client का अपना request ID idempotency नहीं देता और status query endpoint उपलब्ध नहीं है।
Rejected input, authentication या quota पहले ठीक करें। 429 पर backoff से retry तभी करें जब rejection की पुष्टि हो। BetterToken n से गुणा की गई सबसे ऊंची लागू unit price तक preauthorize कर सकता है; delivered 1K image का balance पर्याप्त होने पर भी hold कम पड़ सकता है और 403 के साथ error.code=insufficient_user_quota मिल सकता है। Final charge delivered images पर निर्भर है।
कनेक्शन जाँच सूची
- Base URL
https://bettertoken.ai/v1है। - हेडर में
Authorization: Bearer sk-...शामिल है। - अनुरोध
multipart/form-dataउपयोग करता है। - इस अनुरोध के लिए उपलब्ध Model ID चुनें:
gpt-image-2,gpt-image-2.5-flareयाgpt-image-2.5-sunburst। - एक संदर्भ के लिए
imageऔर कई के लिए दोहराए गएimage[]फ़ील्ड उपयोग करें। - मास्क संपादन में एक
maskभेजें, जिसका फ़ॉर्मैट और आकार मूल चित्र जैसा हो तथा Alpha चैनल मौजूद हो। response_format=b64_jsonउपयोग करें।output_formatअनुरोधित मान है; सहेजने से पहले बाइट्स से वास्तविक फ़ॉर्मैट पहचानें।nमें1से10तक पूर्णांक दे सकते हैं।1से शुरू करें; लौटे चित्र कम हो सकते हैं।sizeछोड़ सकते हैं,autoदे सकते हैं या नियमों के अनुरूप आकार भर सकते हैं।- HTTP क्लाइंट चित्र बनने के लिए कई मिनट तक प्रतीक्षा कर सकता है।
- लॉग और त्रुटि के स्क्रीनशॉट में API Key या पूरा base64 चित्र उजागर नहीं होता।
संबंधित दस्तावेज
/v1/images/editsप्राधिकरण
Bearer authentication header of the form Bearer <token>, where <token> is your auth token.
बॉडी multipart/form-data
GPT Image Model ID. One BetterToken API Key works with all available models.
"gpt-image-2"Instructions for editing the image or generating from reference images.
"Use this reference image to create a more polished square product hero image while preserving the main style."The reference image field for a single input image.
Repeat this field for each input when sending multiple reference images.
An optional single mask file. The mask and original image must have the same format and dimensions, each file must be smaller than 50 MB, and the mask must contain an Alpha channel. For maximum compatibility, use PNG with an Alpha channel for both files. With multiple image[] inputs, the mask applies to the first image. Availability depends on the model and size combination. Ordinary reference-image edits do not require a mask. A mask guides generation but does not guarantee that pixels outside the target area remain unchanged; review the result.
Allowed range: 1–10; start with 1. Fewer images may be delivered than requested. Iterate data[] and use actual-delivery billing.
"1"Omit or use auto for automatic sizing. For WxH, both dimensions must be positive multiples of 16, each at most 3840, aspect ratio at most 3:1, total pixels 655360–8294400. Examples are not an exhaustive enum; 2080x2080 also meets the geometry rules. Inspect actual decoded dimensions. Valid parameters do not guarantee an available service.
"1024x1024"["auto","1024x1024","2048x2048","2080x2080","3840x2160"]Requested output format; png is recommended, but the response is not guaranteed to have been transcoded to PNG. Detect PNG, JPEG, or WebP from actual bytes before choosing an extension. Convert locally if a specific format is required.
"png"gpt-image-2 accepts auto, low, medium, high. The two 2.5 models also accept xhigh and max. Omit for the model default; JSON null is treated as omitted, while multipart requires a nonempty string. Valid values do not guarantee an available service for every model, size, and editing combination. Acceptance does not guarantee that a higher setting improves image quality.
"auto"प्रतिक्रिया
200BetterToken billing extension. Settlement follows the actual delivered pixels and billing tier, which can differ from the requested tier. Amounts are decimal strings; use the returned values.
OpenAI-compatible image response. Iterate data[] and read each image from b64_json. Fewer images may be delivered than requested.
1710000000Show child attributes
An optional revised prompt.
BetterToken billing extension. Settlement follows the actual delivered pixels and billing tier, which can differ from the requested tier. Amounts are decimal strings; use the returned values.
Show child attributes
Settlement currency; do not assume a fixed currency.
Final request amount as a decimal string; use decimal arithmetic for calculations.
Delivered image indices, actual dimensions, billing tiers, and per-image amounts. index is zero-based and corresponds to data[].
Settled amount for this image as a decimal string.
Resolution tier inferred from the request; it may differ from the actual billing tier.
{
"created": 1710000000,
"data": [
{
"b64_json": "iVBORw0KGgoAAAANSUhEUgAA...(truncated)",
"revised_prompt": "<string>"
}
],
"bettertoken_image_billing": {
"currency": "<string>",
"settled_amount": "<string>",
"outputs": [
{
"index": 0,
"width": 1,
"height": 1,
"billing_resolution": "1K",
"currency": "<string>",
"amount": "<string>"
}
],
"partial": false,
"requested_resolution": "<string>"
}
}400Invalid request format, missing parameters, malformed JSON or multipart data, or invalid dimensions.
Show child attributes
"invalid request""invalid_request_error""invalid_request""insufficient quota"{
"error": {
"message": "invalid request",
"type": "invalid_request_error",
"code": "invalid_request"
},
"message": "insufficient quota"
}401The BetterToken API Key is missing or invalid.
Show child attributes
"invalid request""invalid_request_error""invalid_request""insufficient quota"{
"error": {
"message": "invalid request",
"type": "invalid_request_error",
"code": "invalid_request"
},
"message": "insufficient quota"
}402The account has insufficient credit or balance.
Show child attributes
"invalid request""invalid_request_error""invalid_request""insufficient quota"{
"error": {
"message": "invalid request",
"type": "invalid_request_error",
"code": "invalid_request"
},
"message": "insufficient quota"
}403Insufficient balance or token quota: error.code is insufficient_user_quota. Funds must also cover the reservation of the highest-tier unit price times the image-count upper bound, before actual-delivery settlement. Inspect error.code rather than treating every 403 as an authorization error.
{
"error": {
"code": "insufficient_user_quota",
"message": "Insufficient balance or token quota."
}
}429A rate or concurrency limit was reached, or the upstream service is busy.
Show child attributes
"invalid request""invalid_request_error""invalid_request""insufficient quota"{
"error": {
"message": "invalid request",
"type": "invalid_request_error",
"code": "invalid_request"
},
"message": "insufficient quota"
}500The gateway or upstream service returned an error.
Show child attributes
"invalid request""invalid_request_error""invalid_request""insufficient quota"{
"error": {
"message": "invalid request",
"type": "invalid_request_error",
"code": "invalid_request"
},
"message": "insufficient quota"
}
