इसे छोड़कर कंटेंट पर जाएं
मुफ़्त साइन अप करें
GPT Image

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-2
n=1
response_format=b64_json
output_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उदाहरण
modelgpt-image-2
promptUsing this reference image, create a more polished square product key visual while preserving the subject style.
n1
size1024x1024
response_formatb64_json
output_formatpng
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उदाहरण
modelgpt-image-2
promptCombine these reference images into a promotional poster with a consistent style.
n1
size1024x1024
response_formatb64_json
output_formatpng
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 Path
from 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 base64
from 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अनुपातउपयोग
autoAutoअपने-आप आकार चयन
1024x10241:1वर्गाकार इमेज, avatar, cover, asset
1536x10243:2landscape poster, banner, scene
1024x15362:3portrait mobile cover और poster
1536x11524:3मानक landscape इमेज, product image, content graphic
1152x15363:4मानक portrait इमेज, mobile cover, vertical poster
2048x20481:1high-resolution वर्गाकार इमेज
2048x115216:9high-resolution landscape इमेज
3840x216016:94K landscape इमेज
2160x38409:164K 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अर्थ
currencySettlement currency; यह हमेशा USD या CNY नहीं होती।
settled_amountइस request की कुल settled रकम, decimal string।
requested_resolutionRequest से पहचाना गया resolution tier।
outputs[].indexdata[] से जुड़ा zero-based image index।
outputs[].width, heightImage की वास्तविक चौड़ाई और ऊंचाई, pixels में।
outputs[].billing_resolutionImage का अंतिम billing tier।
outputs[].currency, amountImage की 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 चित्र उजागर नहीं होता।

संबंधित दस्तावेज

POST/v1/images/edits

प्राधिकरण

Authorizationstringheaderआवश्यक

Bearer authentication header of the form Bearer <token>, where <token> is your auth token.

बॉडी multipart/form-data

modelenum<string>डिफ़ॉल्ट: gpt-image-2आवश्यक

GPT Image Model ID. One BetterToken API Key works with all available models.

उपलब्ध विकल्प: gpt-image-2, gpt-image-2.5-flare, gpt-image-2.5-sunburst

उदाहरण:"gpt-image-2"
promptstringआवश्यक

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."
imagestring<binary>

The reference image field for a single input image.

image[]string[]

Repeat this field for each input when sending multiple reference images.

maskstring<binary>

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.

ninteger | stringडिफ़ॉल्ट: 1

Allowed range: 1–10; start with 1. Fewer images may be delivered than requested. Iterate data[] and use actual-delivery billing.

उदाहरण:"1"
sizestringडिफ़ॉल्ट: 1024x1024

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"]
response_formatenum<string>डिफ़ॉल्ट: b64_json

Use b64_json.

उपलब्ध विकल्प: b64_json

उदाहरण:"b64_json"
output_formatstringडिफ़ॉल्ट: png

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"
qualityenum<string>डिफ़ॉल्ट: auto

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, low, medium, high, xhigh, max

उदाहरण:"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.

createdinteger
उदाहरण:1710000000
dataobject[]
Show child attributes

Base64-encoded image content.

उदाहरण:"iVBORw0KGgoAAAANSUhEUgAA...(truncated)"

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[].

आवश्यक सीमा: x >= 0

आवश्यक सीमा: x >= 1

आवश्यक सीमा: x >= 1

उपलब्ध विकल्प: 1K, 2K, 4K

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.
errorobject
Show child attributes
उदाहरण:"invalid request"
उदाहरण:"invalid_request_error"
उदाहरण:"invalid_request"
messagestring
उदाहरण:"insufficient quota"
उदाहरण
{
  "error": {
    "message": "invalid request",
    "type": "invalid_request_error",
    "code": "invalid_request"
  },
  "message": "insufficient quota"
}
401The BetterToken API Key is missing or invalid.
errorobject
Show child attributes
उदाहरण:"invalid request"
उदाहरण:"invalid_request_error"
उदाहरण:"invalid_request"
messagestring
उदाहरण:"insufficient quota"
उदाहरण
{
  "error": {
    "message": "invalid request",
    "type": "invalid_request_error",
    "code": "invalid_request"
  },
  "message": "insufficient quota"
}
402The account has insufficient credit or balance.
errorobject
Show child attributes
उदाहरण:"invalid request"
उदाहरण:"invalid_request_error"
उदाहरण:"invalid_request"
messagestring
उदाहरण:"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.
errorobject
Show child attributes
उदाहरण
{
  "error": {
    "code": "insufficient_user_quota",
    "message": "Insufficient balance or token quota."
  }
}
429A rate or concurrency limit was reached, or the upstream service is busy.
errorobject
Show child attributes
उदाहरण:"invalid request"
उदाहरण:"invalid_request_error"
उदाहरण:"invalid_request"
messagestring
उदाहरण:"insufficient quota"
उदाहरण
{
  "error": {
    "message": "invalid request",
    "type": "invalid_request_error",
    "code": "invalid_request"
  },
  "message": "insufficient quota"
}
500The gateway or upstream service returned an error.
errorobject
Show child attributes
उदाहरण:"invalid request"
उदाहरण:"invalid_request_error"
उदाहरण:"invalid_request"
messagestring
उदाहरण:"insufficient quota"
उदाहरण
{
  "error": {
    "message": "invalid request",
    "type": "invalid_request_error",
    "code": "invalid_request"
  },
  "message": "insufficient quota"
}