> [Site index](https://perfectvector.com/llms.txt) — available pages and descriptions.

# Python और API से इमेज वेक्टराइज़ करके SVG सेव करें

> Python से PNG, JPEG या WebP को वेक्टराइज़ेशन API पर भेजें। SVG सेव करें, टेस्ट रिस्पॉन्स जाँचें और कनेक्शन टूटने पर पुराना नतीजा वापस पाएँ।

Source: https://perfectvector.com/hi/blog/vectorize-image-python-api

Language: hi
Author: Jay
Published: 2026-11-09T08:00:00+09:00

Python स्क्रिप्ट से रास्टर इमेज PerfectVector वेक्टराइज़ेशन API को भेजकर लौटे हुए SVG को मूल फ़ाइल के साथ सेव कर सकते हैं। नीचे का उदाहरण डाउनलोड से पहले कन्वर्ज़न ID प्रिंट करता है। कनेक्शन टूटने पर नतीजा वापस पाने के दो तरीके हैं: यह ID और आपके चुने अनुरोध की की।

## टेस्ट की और एक इमेज तैयार करें

[Developer API पेज](https://perfectvector.com/account/developers) से टेस्ट की बनाएँ। टर्मिनल में इसे `PERFECTVECTOR_API_KEY` के रूप में सेट करें। इसे स्क्रिप्ट या वर्ज़न कंट्रोल में न रखें। प्रोजेक्ट के Python एनवायरनमेंट में `python -m pip install requests` से Requests इंस्टॉल करें।

पहली बार एक फ़्रेम वाली PNG, JPEG या WebP लें, जैसे 512 × 512 पिक्सेल का `logo.png`। API 5 MiB तक की फ़ाइल स्वीकार करती है, जिसकी हर भुजा 100 से 4,096 पिक्सेल हो। टेस्ट मोड अपलोड जाँचता है और बिना शुल्क के “TEST MODE” लिखा तय SVG लौटाता है। उस फ़ाइल से अनुरोध और डाउनलोड की जाँच करें। अपनी इमेज की ट्रेसिंग की गुणवत्ता देखने के लिए लाइव कन्वर्ज़न चलाएँ। [PerfectVector API टेस्ट मोड](https://perfectvector.com/docs/api/test-mode)

हमने Python स्क्रिप्ट टेस्ट की से चलाई। डाउनलोड किया हुआ SVG सफलतापूर्वक पार्स हुआ। उसी अनुरोध की से वही अपलोड दोहराने पर दोनों रन में एक ही कन्वर्ज़न ID मिली।

**वर्कफ़्लो का चित्र**

![कंप्यूटर पर रखी रास्टर फ़ाइल पहचान टैग वाले क्लाउड अनुरोध से होकर डिस्क पर वेक्टर फ़ाइल के रूप में लौटती है।](https://perfectvector.com/blog/vectorize-image-python-api/workflow.webp)

*अनुरोध की को अपलोड के साथ रखें और SVG डाउनलोड करने से पहले कन्वर्ज़न ID सेव करें।*

## Python से इमेज भेजें और SVG सेव करें

नीचे का कोड `vectorize.py` नाम से सेव करें। `files` आर्ग्युमेंट Requests को मल्टीपार्ट अपलोड बनाने के लिए कहता है। इससे मूल बाइट `image` फ़ॉर्म फ़ील्ड में जाते हैं। [Requests में मल्टीपार्ट अपलोड](https://requests.readthedocs.io/en/latest/user/quickstart/#post-a-multipart-encoded-file)

```python
import os
import sys
import time
from pathlib import Path

import requests

API = "https://api.perfectvector.com/v1"
AUTH = {"Authorization": f"Bearer {os.environ['PERFECTVECTOR_API_KEY']}"}


def checked(response):
    content_type = response.headers.get("Content-Type", "").split(";")[0]
    if response.status_code >= 400 and content_type == "application/problem+json":
        problem = response.json()
        raise RuntimeError(f"{problem['code']} (request {problem['request_id']})")
    response.raise_for_status()
    return response


def vectorize(source, destination, operation_id):
    with source.open("rb") as image:
        response = checked(requests.post(
            f"{API}/vectorizations",
            headers={**AUTH, "Idempotency-Key": operation_id, "Prefer": "wait=10"},
            files={"image": (source.name, image)},
            timeout=30,
        ))

    conversion = response.json()
    conversion_id = conversion["id"]
    print(f"Conversion ID: {conversion_id}", flush=True)
    deadline = time.monotonic() + 720
    while conversion["status"] in {"queued", "processing"}:
        if time.monotonic() >= deadline:
            raise TimeoutError(f"Stopped polling {conversion_id}")
        time.sleep(2)
        conversion = checked(requests.get(
            f"{API}/vectorizations/{conversion_id}", headers=AUTH, timeout=30,
        )).json()

    if conversion["status"] != "succeeded":
        error = conversion["results"][0].get("error") or {}
        raise RuntimeError(f"{conversion_id}: {error.get('code', conversion['status'])}")

    svg = checked(requests.get(
        f"{API}/vectorizations/{conversion_id}/result", headers=AUTH, timeout=30,
    ))
    if svg.headers.get("Content-Type", "").split(";")[0] != "image/svg+xml":
        raise ValueError("Expected an SVG response")
    partial = destination.with_suffix(".svg.part")
    partial.write_bytes(svg.content)
    partial.replace(destination)
    print(f"{destination} saved ({len(svg.content)} bytes)")


if __name__ == "__main__":
    source = Path(sys.argv[1])
    if source.suffix.lower() not in {".png", ".jpg", ".jpeg", ".webp"}:
        raise SystemExit("Choose a PNG, JPEG or WebP file")
    if source.stat().st_size > 5 * 1024 * 1024:
        raise SystemExit("Choose a file no larger than 5 MiB")
    vectorize(source, source.with_suffix(".svg"), sys.argv[2])
```

इसे उस फ़ोल्डर से चलाएँ जहाँ `logo.png` है। कन्वर्ज़न के लिए एक ID चुनें और कमांड चलाने से पहले उसे लिख लें। API इसे *आइडेम्पोटेंसी की* कहती है। एक ही फ़ाइल दोबारा भेजने पर यह अनुरोध को उसी कन्वर्ज़न से जोड़ती है। उदाहरण में `logo-asset-0001` इस्तेमाल हुआ है। अलग इमेज या ऑपरेशन के लिए अलग मान रखें। [PerfectVector में सुरक्षित रीट्राई](https://perfectvector.com/docs/api/idempotency)

```bash
python vectorize.py logo.png logo-asset-0001
```

टेस्ट सफल हो तो कन्वर्ज़न ID और बाइट की संख्या दिखेगी और `logo.png` के साथ `logo.svg` बनेगा। `logo.svg` खोलकर देख लें कि सैंपल दिखता है। नया डाउनलोड पूरा होने पर स्क्रिप्ट उसी नाम का मौजूदा SVG बदल देती है। पहली बार अस्थायी फ़ोल्डर में चलाएँ।

स्क्रिप्ट सर्वर से कहती है कि अधूरा कन्वर्ज़न लौटाने से पहले 10 सेकंड तक इंतज़ार करे। यह `queued` और `processing` कन्वर्ज़न का स्टेटस करीब हर दो सेकंड में जाँचती है और `succeeded` होने पर ही डाउनलोड करती है। टेस्ट कन्वर्ज़न तुरंत पूरा होता है। लाइव वर्कर में इस्तेमाल करने से पहले किसी लोकल टेस्ट डेटा से अधूरा रिस्पॉन्स बनाकर स्टेटस जाँचने वाला हिस्सा परखें। [PerfectVector में इंतज़ार और असिंक्रोनस प्रोसेसिंग](https://perfectvector.com/docs/api/waiting-and-async)

## टाइमआउट के बाद कन्वर्ज़न का नतीजा वापस पाएँ

अगर Python ने कन्वर्ज़न ID प्रिंट की है, तो `GET /v1/vectorizations/{id}` से उसका स्टेटस जाँचें। सफल होने पर उसी अकाउंट और मोड में `GET /v1/vectorizations/{id}/result` से SVG डाउनलोड करें। कन्वर्ज़न पूरा होने के 24 घंटे बाद नतीजे का लिंक एक्सपायर हो जाता है। बाद में ज़रूरत हो तो फ़ाइल अपने स्टोरेज में कॉपी कर लें। [PerfectVector के नतीजे डाउनलोड करना](https://perfectvector.com/docs/api/results)

अगर Python ने ID प्रिंट करने से पहले काम रोक दिया, तो पहले अनुरोध के 24 घंटे के भीतर उन्हीं फ़ाइल बाइट और उसी आइडेम्पोटेंसी की से कमांड दोबारा चलाएँ। API ने पहला अनुरोध स्वीकार किया था तो वही कन्वर्ज़न लौटेगा। इस अवधि के बाद उसी की से नया कन्वर्ज़न शुरू हो सकता है। [PerfectVector में सुरक्षित रीट्राई](https://perfectvector.com/docs/api/idempotency)

| क्या दिखता है                                       | क्या जाँचें                                                                                                                    |
| --------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
| `401 invalid_api_key`                               | Python के एनवायरनमेंट में मान्य टेस्ट की सेट करें। पुरानी की रद्द कर दी हो तो उसी मोड में नई बनाएँ।                            |
| `415 unsupported_media_type` या `422 invalid_image` | मूल फ़ाइल खोलकर असली फ़ॉर्मैट और पिक्सेल आयाम जाँचें। सही की गई इमेज सेव करें और उसके अपलोड के लिए अलग की रखें।                |
| कन्वर्ज़न ID के बाद `processing_failed`             | API ने कन्वर्ज़न रिकॉर्ड बनाया, लेकिन इमेज ट्रेस नहीं हो सकी। मूल फ़ाइल जाँचें और ठीक की हुई फ़ाइल नए ऑपरेशन के रूप में भेजें। |
| `409 idempotency_conflict`                          | उसी की के साथ अलग इमेज बाइट या विकल्प इस्तेमाल हुए। मूल फ़ाइल से नतीजा वापस पाएँ या नए इनपुट को नई की दें।                     |

सपोर्ट से संपर्क करने के लिए एक्सेप्शन की अनुरोध ID या स्क्रिप्ट की कन्वर्ज़न ID सँभालकर रखें। किन त्रुटियों पर सुरक्षित रीट्राई किया जा सकता है, इसकी जानकारी [API त्रुटि गाइड](https://perfectvector.com/docs/api/errors) में है।

## अक्सर पूछे जाने वाले सवाल

### क्या यही Python स्क्रिप्ट JPEG या WebP भी वेक्टराइज़ कर सकती है?

हाँ। पहले आर्ग्युमेंट में JPEG या WebP दें। फ़ाइल के आकार, आयाम और एक फ़्रेम वाली शर्तें वही हैं। आउटपुट का एक्सटेंशन SVG होगा।

### क्या इस Python उदाहरण को पूरे फ़ोल्डर के लिए चला सकते हैं?

पहले एक फ़ाइल का अपलोड और डाउनलोड जाँचें। पूरे फ़ोल्डर के लिए हर फ़ाइल की की, कन्वर्ज़न ID और आउटपुट पाथ स्थायी क्यू में दर्ज करें। PerfectVector डिफ़ॉल्ट तौर पर एक अकाउंट में एक समय पर एक लाइव कन्वर्ज़न चलाता है। अगला अनुरोध भेजने से पहले मौजूदा कन्वर्ज़न पूरा होने दें। [PerfectVector API की सीमाएँ](https://perfectvector.com/docs/api/limits)

## स्रोत

1. [Requests — शुरुआत](https://requests.readthedocs.io/en/latest/user/quickstart/) — मल्टीपार्ट फ़ाइलें, टाइमआउट और बाइट रिस्पॉन्स।
2. [PerfectVector — टेस्ट मोड](https://perfectvector.com/docs/api/test-mode) — अपलोड सीमाएँ, तय टेस्ट SVG और टेस्ट क्रेडिट के नियम।
3. [PerfectVector — इंतज़ार और असिंक्रोनस प्रोसेसिंग](https://perfectvector.com/docs/api/waiting-and-async) — सर्वर की प्रतीक्षा अवधि, स्टेटस जाँचना और टेस्ट अनुरोध कब पूरा होता है।
4. [PerfectVector — आइडेम्पोटेंसी](https://perfectvector.com/docs/api/idempotency) — की की वैधता, दायरा, टकराव और दोबारा भेजने के नियम।
5. [PerfectVector — नतीजे](https://perfectvector.com/docs/api/results) — नतीजे का एंडपॉइंट, ऑथेंटिकेशन और एक्सपायरी।
6. [PerfectVector — त्रुटियाँ](https://perfectvector.com/docs/api/errors) — HTTP समस्या कोड और रिकवरी के निर्देश।
7. [PerfectVector — सीमाएँ](https://perfectvector.com/docs/api/limits) — लाइव कन्वर्ज़न की डिफ़ॉल्ट समवर्ती सीमा और फ़ाइल की शर्तें।

***

[PerfectVector API पेज से टेस्ट की लें](https://perfectvector.com/hi/api), अपनी एक इमेज पर स्क्रिप्ट चलाएँ और देखें कि सेव किया हुआ SVG खुलता है और टर्मिनल में कन्वर्ज़न ID दिखती है।

## Explore

- [Read the HTML article](https://perfectvector.com/hi/blog/vectorize-image-python-api)
- [2026 के सबसे अच्छे AI इमेज वेक्टराइज़र: हर काम के लिए हमारी पसंद](https://perfectvector.com/hi/blog/best-ai-vectorizers-2026.md)
- [Claude Code में फ़ोल्डर की इमेज से SVG बनाएँ](https://perfectvector.com/hi/blog/batch-vectorize-images-claude-code-mcp.md)
- [Product facts](https://perfectvector.com/product-facts.md)
- [FAQ](https://perfectvector.com/faq.md)
- [Pricing and credits](https://perfectvector.com/pricing.md)
