การทำงานกับภาพวาด CAD มักหมายถึงการแปลงไฟล์ DWG ให้เป็นภาพที่เป็นมิตรกับเว็บ เช่น PNG. Aspose.BarCode Cloud SDK for Python ทำให้การแปลงนี้ง่ายขึ้นโดยการส่งการประมวลผลไปยังคลาวด์ ในบทแนะนำนี้เราจะอธิบายวิธีตั้งค่า SDK, การรับรองความถูกต้อง, การเขียนโค้ด Python เพื่อแปลง DWG เป็น PNG, และทำให้ได้ผลลัพธ์เดียวกันด้วยคำสั่ง cURL ดิบ เพื่อให้คุณสามารถรวมการแสดงผล DWG เข้าไปในแอปพลิเคชัน Python ของคุณได้อย่างง่ายดาย.
การแปลง DWG เป็น PNG ใน Python - ขั้นตอน
- กำหนดข้อมูลประจำตัว: ตั้งค่า client ID และ client secret ของคุณเพื่อให้ SDK สามารถตรวจสอบสิทธิ์กับ Aspose cloud ได้.
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
- เริ่มต้น API Client: สร้างอ็อบเจ็กต์
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()
- เรียกใช้ Endpoint การแปลง: ใช้
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
- รับ Access Token
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 for 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 ใน Python กลายเป็นเรื่องง่ายด้วยพลังของ Aspose.BarCode Cloud SDK for Python. ตามขั้นตอนข้างต้น คุณสามารถรวมการแปลงจากเวกเตอร์เป็นแรสเตอร์เข้าสู่บริการแบ็กเอนด์ใด ๆ อัตโนมัติการประมวลผลเป็นชุด หรือสร้างตัวอย่างพรีวิวตามความต้องการสำหรับภาพวาด CAD. อย่าลืมรับใบอนุญาตชั่วคราวที่ถูกต้องจาก Aspose temporary license page สำหรับการพัฒนาและการทดสอบ และพิจารณาซื้อใบอนุญาตเต็มรูปแบบสำหรับการใช้งานในสภาพแวดล้อมการผลิต. ขอให้สนุกกับการเขียนโค้ด!
คำถามที่พบบ่อย
-
SDK รองรับรูปแบบไฟล์ใดสำหรับการแปลง?
ปลายทางการแปลงในคลาวด์รองรับรูปแบบเวกเตอร์และแรสเตอร์หลายประเภท รวมถึง DWG, DXF, SVG, PDF, และอื่น ๆ อีกมากมาย ดูที่ เอกสาร สำหรับรายการเต็ม. -
การแปลง DWG เป็น PNG ใน Python ปลอดภัยต่อเธรดหรือไม่?
ใช่, แต่ละอินสแตนซ์BarcodeApiสามารถใช้พร้อมกันได้ตราบใดที่คุณจัดการวัตถุ credential แยกต่างหากสำหรับแต่ละเธรด.