العمل مع رسومات CAD غالبًا ما يعني تحويل ملفات DWG إلى صور صديقة للويب مثل PNG. يجعل Aspose.BarCode Cloud SDK for Python هذا التحويل بسيطًا عن طريق نقل المعالجة إلى السحابة. في هذا البرنامج التعليمي سنظهر لك كيفية إعداد SDK، والمصادقة، وكتابة كود Python لتحويل DWG إلى PNG، وتحقيق النتيجة نفسها باستخدام طلب cURL خام، حتى تتمكن من دمج عرض DWG في تطبيقات Python الخاصة بك بسهولة.
تحويل DWG إلى PNG في Python - الخطوات
- تكوين بيانات الاعتماد: قم بتعيين معرف العميل وسر العميل حتى يتمكن SDK من المصادقة مع سحابة Aspose.
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
- تهيئة عميل API: إنشاء كائن
Configuration، وتعيين بيانات الاعتماد، وبناء كائناتApiClientوBarcodeApi.
config = Configuration()
config.client_id = client_id
config.client_secret = client_secret
api_client = ApiClient(configuration=config)
barcode_api = BarcodeApi(api_client)
- قراءة ملف DWG: افتح ملف DWG المصدر في وضعية ثنائية واقرأ محتوياته إلى الذاكرة.
with open("sample.dwg", "rb") as file_stream:
dwg_bytes = file_stream.read()
- استدعاء نقطة النهاية للتحويل: استخدم
convert_imageوحدد"png"كتنسيق الهدف. تُعيد الطريقة بيانات PNG كمصفوفة بايت.
conversion_response = barcode_api.convert_image(
file=dwg_bytes,
format="png"
)
- احفظ إخراج PNG: اكتب البايتات المسترجعة إلى ملف بامتداد
.png.
with open("sample.png", "wb") as out_file:
out_file.write(conversion_response)
لمزيد من التفاصيل حول طريقة convert_image، راجع مرجع API.
تحويل DWG إلى PNG في Python - مثال عملي كامل
المثال التالي يوضح سير العمل الكامل لتحويل DWG إلى PNG في Python باستخدام Aspose.BarCode Cloud SDK.
import os
from asposebarcodecloud import Configuration, ApiClient, BarcodeApi, ApiException
# -------------------- Configuration --------------------
# Replace with your actual Aspose.BarCode Cloud credentials
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
config = Configuration()
config.client_id = client_id
config.client_secret = client_secret
# Initialize API client
api_client = ApiClient(configuration=config)
barcode_api = BarcodeApi(api_client)
# -------------------- File Paths --------------------
input_path = "sample.dwg" # Path to the source DWG file
output_path = "sample.png" # Desired PNG output path
# -------------------- Conversion --------------------
try:
# Read the DWG file into memory
with open(input_path, "rb") as file_stream:
dwg_bytes = file_stream.read()
# The Aspose.BarCode Cloud SDK provides a generic image conversion endpoint.
# Here we invoke it, specifying the target format as PNG.
# The method name and parameters are based on the SDK's conversion API.
conversion_response = barcode_api.convert_image(
file=dwg_bytes, # Binary content of the DWG file
format="png" # Target image format
)
# Write the resulting PNG bytes to the output file
with open(output_path, "wb") as out_file:
out_file.write(conversion_response)
print(f"Conversion successful: '{input_path}' → '{output_path}'")
except ApiException as api_err:
print(f"API error during conversion: {api_err}")
except Exception as err:
print(f"Unexpected error: {err}")
finally:
# Explicitly close the API client session if needed
if hasattr(api_client, "close"):
api_client.close()
ملاحظة: يوضح مثال الكود هذا الوظيفة الأساسية. قبل استخدامه في مشروعك، تأكد من تحديث أي مسارات ملفات وقيم التكوين لتتناسب مع بيئتك الفعلية، وتحقق من تثبيت جميع الاعتمادات المطلوبة بشكل صحيح، واختبر بدقة في بيئة التطوير الخاصة بك. إذا واجهت أي مشكلات، يرجى الرجوع إلى الوثائق الرسمية أو التواصل مع فريق الدعم للحصول على المساعدة.
تحويل DWG إلى PNG عبر REST API باستخدام cURL
فيما يلي سلسلة من أوامر cURL التي تقوم بنفس التحويل باستخدام واجهة REST.
- الحصول على رمز وصول
curl -X POST "https://api.aspose.cloud/connect/token" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
الاستجابة تحتوي على access_token.
- تحميل ملف DWG
curl -X PUT "https://api.aspose.cloud/v3.0/barcode/storage/file/sample.dwg" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary "@sample.dwg"
- تنفيذ التحويل
curl -X POST "https://api.aspose.cloud/v3.0/barcode/convert?format=png" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-H "Content-Type: application/octet-stream" \
--data-binary "@sample.dwg" \
-o sample.png
- تنزيل نتيجة PNG (اختياري إذا استخدمت
-oأعلاه)
curl -X GET "https://api.aspose.cloud/v3.0/barcode/storage/file/sample.png" \
-H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
-o sample.png
للحصول على قائمة كاملة بالمعلمات، راجع وثائق API الرسمية.
تثبيت وتكوين Aspose.BarCode Cloud SDK لـ Python
يتم توزيع SDK عبر PyPI. قم بتثبيته باستخدام الأمر التالي:
pip install aspose-barcode-cloud
المتطلبات المسبقة: Python 3.6 أو أحدث، حساب Aspose نشط، واعتمادات عميل صالحة. يتطلب SDK اتصالًا بالإنترنت للوصول إلى نقاط النهاية السحابية لـ Aspose.
يمكنك أيضًا تنزيل الحزمة مباشرةً من صفحة الإصدار.
تكوين معلمات التحويل من DWG إلى PNG
تقبل عملية التحويل معامل format الذي يحدد نوع صورة الإخراج. في مثالنا قمنا بتعيينه إلى "png":
conversion_response = barcode_api.convert_image(
file=dwg_bytes,
format="png"
)
معلمات اختيارية أخرى (مثل outPath و storage أو إعدادات جودة الصورة) موثقة في مرجع API. قم بتعديلها حسب الحاجة لتتناسب مع متطلبات مشروعك.
الخلاصة
تحويل DWG إلى PNG في بايثون يصبح سهلًا بفضل قوة Aspose.BarCode Cloud SDK for Python. باتباع الخطوات المذكورة أعلاه، يمكنك دمج تحويل المتجه إلى نقطية في أي خدمة خلفية، أتمتة المعالجة الدفعية، أو إنشاء معاينات حسب الطلب لرسومات CAD. تذكر الحصول على ترخيص مؤقت صالح من Aspose temporary license page للتطوير والاختبار، وفكر في شراء ترخيص كامل للاستخدام في الإنتاج. برمجة سعيدة!
FAQs
-
ما هي صيغ الملفات التي يدعمها SDK للتحويل؟
يتعامل نقطة النهاية السحابية للتحويل مع العديد من صيغ المتجهات والنقوش، بما في ذلك DWG، DXF، SVG، PDF، وأكثر. راجع التوثيق للحصول على القائمة الكاملة. -
هل تحويل DWG إلى PNG في Python آمن للخطوط المتعددة؟
نعم، يمكن استخدام كل كائنBarcodeApiبشكل متزامن طالما أنك تدير كائنات الاعتماد المنفصلة لكل خيط.