Working with CAD drawings often means turning DWG files into web‑friendly images such as PNG. The Aspose.BarCode Cloud SDK for Python makes this conversion simple by offloading the processing to the cloud. In this tutorial we’ll show you how to set up the SDK, authenticate, write the Python code to convert a DWG to PNG, and achieve the same result with a raw cURL request, so you can integrate DWG rendering into your Python applications with ease.
DWG to PNG Conversion in Python - Steps
- Configure Credentials: Set your client ID and client secret so the SDK can authenticate with the Aspose cloud.
client_id = "YOUR_CLIENT_ID"
client_secret = "YOUR_CLIENT_SECRET"
- Initialize API Client: Create a
Configurationobject, assign the credentials, and build theApiClientandBarcodeApiinstances.
config = Configuration()
config.client_id = client_id
config.client_secret = client_secret
api_client = ApiClient(configuration=config)
barcode_api = BarcodeApi(api_client)
- Read the DWG File: Open the source DWG file in binary mode and read its contents into memory.
with open("sample.dwg", "rb") as file_stream:
dwg_bytes = file_stream.read()
- Call the Conversion Endpoint: Use
convert_imageand specify"png"as the target format. The method returns the PNG data as a byte array.
conversion_response = barcode_api.convert_image(
file=dwg_bytes,
format="png"
)
- Save the PNG Output: Write the returned bytes to a file with a
.pngextension.
with open("sample.png", "wb") as out_file:
out_file.write(conversion_response)
For more details on the convert_image method, see the API Reference.
DWG to PNG Conversion in Python - Full Working Sample
The following example demonstrates the complete workflow for DWG to PNG conversion in Python using the 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()
Note: This code example demonstrates the core functionality. Before using it in your project, make sure to update any file paths and configuration values to match your actual environment, verify that all required dependencies are properly installed, and test thoroughly in your development environment. If you encounter any issues, please refer to the official documentation or reach out to the support team for assistance.
Convert DWG to PNG via REST API Using cURL
Below is a series of cURL commands that perform the same conversion using the REST interface.
-
Obtain an 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"The response contains
access_token. -
Upload the DWG File
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" -
Execute the Conversion
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 -
Download the PNG Result (optional if you used
-oabove)curl -X GET "https://api.aspose.cloud/v3.0/barcode/storage/file/sample.png" \ -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \ -o sample.png
For a full list of parameters, see the official API documentation.
Installing and Configuring Aspose.BarCode Cloud SDK for Python
The SDK is distributed via PyPI. Install it with the following command:
pip install aspose-barcode-cloud
Prerequisites: Python 3.6 or newer, an active Aspose account, and valid client credentials. The SDK requires internet access to reach the Aspose cloud endpoints.
You can also download the package directly from the release page.
Configuring Conversion Parameters for DWG to PNG
The conversion call accepts a format parameter that determines the output image type. In our example we set it to "png":
conversion_response = barcode_api.convert_image(
file=dwg_bytes,
format="png"
)
Other optional parameters (such as outPath, storage, or image quality settings) are documented in the API Reference. Adjust them as needed to match your project’s requirements.
Conclusion
DWG to PNG conversion in Python becomes trivial with the power of the Aspose.BarCode Cloud SDK for Python. By following the steps above, you can integrate vector‑to‑raster conversion into any backend service, automate batch processing, or generate on‑demand previews for CAD drawings. Remember to obtain a valid temporary license from the Aspose temporary license page for development and testing, and consider purchasing a full license for production use. Happy coding!
FAQs
-
What file formats does the SDK support for conversion?
The cloud conversion endpoint handles many vector and raster formats, including DWG, DXF, SVG, PDF, and more. Refer to the documentation for the full list. -
Is DWG to PNG conversion in Python thread‑safe?
Yes, eachBarcodeApiinstance can be used concurrently as long as you manage separate credential objects per thread.