Converting medical images from DICOM to JPG is a frequent task when radiology applications need to display studies in web portals or mobile viewers. Aspose.HTML Cloud SDK for Python provides a robust library that handles the heavy lifting on the server side. In this guide you will learn how to implement DICOM to JPG conversion in Python step by step, explore multi‑threaded options, and see how to call the same service with cURL for REST‑based workflows.

The DICOM to JPG Conversion Requirements

Radiology workstations often receive DICOM files that must be rendered as lightweight JPG thumbnails for quick preview. Developers need a solution that:

  • Reads binary DICOM data without relying on external imaging libraries.
  • Converts the image to a widely supported format such as JPG while preserving diagnostic quality.
  • Handles large volumes of files efficiently, ideally with parallel processing.

Manual conversion using desktop tools is time‑consuming and does not scale for server‑side pipelines, making an automated API essential for modern radiology software.

The Approach: Optimized Cloud Conversion Workflow

Aspose.HTML Cloud SDK for Python offers a cloud‑based API that abstracts the conversion logic. The service:

  • Accepts raw DICOM bytes and returns a JPG byte stream.
  • Executes in the Aspose cloud, eliminating the need for local DICOM parsers.
  • Provides configuration options for image quality, resolution, and multi‑threaded execution.

Refer to the official documentation for authentication details and the API reference for the ConvertDocumentRequest class used in this tutorial.

Implementing DICOM to JPG Conversion in Python

Below is a practical, step‑by‑step implementation that you can integrate into any radiology backend.

Install Aspose.HTML Cloud SDK for Python

pip install asposehtmlcloud

Configure API Client with Your Credentials

from asposehtmlcloud import HtmlApi, Configuration

config = Configuration()
config.api_key['api_key'] = api_key               # Replace with your actual API key
config.host = "https://api.aspose.cloud"

html_api = HtmlApi(config)

Load DICOM File Into Memory

with open(input_file, "rb") as f:
    dicom_bytes = f.read()

Create Conversion Request for JPG Output

from asposehtmlcloud import ConvertDocumentRequest

request = ConvertDocumentRequest(
    file=dicom_bytes,
    format="jpg"
)

Write JPG Bytes to Disk

jpg_bytes = html_api.convert_document(request)

with open(output_file, "wb") as out_f:
    out_f.write(jpg_bytes)

With these steps the DICOM to JPG conversion workflow is complete, and you can now integrate the utility into larger pipelines or invoke it from command‑line scripts.

DICOM to JPG Conversion Utility - Complete Code Example

The following example demonstrates the full end‑to‑end process.

import os
from asposehtmlcloud import HtmlApi, Configuration, ConvertDocumentRequest

def dicom_to_jpg(input_file: str, output_file: str, api_key: str):
    # Configure Aspose.HTML Cloud SDK
    config = Configuration()
    config.api_key['api_key'] = api_key               # Replace with your actual API key
    config.host = "https://api.aspose.cloud"

    # Initialize the API client
    html_api = HtmlApi(config)

    # Read the DICOM file into memory
    with open(input_file, "rb") as f:
        dicom_bytes = f.read()

    # Build the conversion request (target format: jpg)
    request = ConvertDocumentRequest(
        file=dicom_bytes,
        format="jpg"
    )

    # Perform conversion – the response is a binary stream of the JPG image
    jpg_bytes = html_api.convert_document(request)

    # Write the resulting JPG to disk
    with open(output_file, "wb") as out_f:
        out_f.write(jpg_bytes)

if __name__ == "__main__":
    # Example usage
    INPUT_DICOM = "sample.dcm"          # Path to your source DICOM file
    OUTPUT_JPG = "sample_converted.jpg" # Desired output path
    API_KEY = "YOUR_ASPOSE_CLOUD_API_KEY"

    dicom_to_jpg(INPUT_DICOM, OUTPUT_JPG, API_KEY)

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.

Multi-Threaded Conversion via REST API using cURL

If you prefer a pure REST approach, the same conversion can be performed with cURL commands. This is useful for command‑line automation or integration with CI pipelines.

  1. Obtain an access token

    curl -X POST "https://api.aspose.cloud/connect/token" \
         -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
    
  2. Upload the DICOM file

    curl -X PUT "https://api.aspose.cloud/v4.0/html/storage/file/sample.dcm" \
         -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
         -H "Content-Type: application/dicom" \
         --data-binary "@sample.dcm"
    
  3. Request conversion to JPG

    curl -X POST "https://api.aspose.cloud/v4.0/html/convert?format=jpg" \
         -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
         -H "Content-Type: application/json" \
         -d '{"FileName":"sample.dcm"}' \
         -o sample_converted.jpg
    
  4. Download the converted JPG (if not saved directly)

    curl -X GET "https://api.aspose.cloud/v4.0/html/storage/file/sample_converted.jpg" \
         -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
         -o sample_converted.jpg
    

These commands illustrate a multi‑threaded conversion workflow where multiple files can be processed in parallel by issuing several upload/convert pairs simultaneously. For full API details, see the official API documentation.

Configuring Conversion Options for DICOM to JPG

The SDK allows you to fine‑tune the output. Common options include:

  • Image Quality - Adjust the JPEG compression level (0‑100).
  • Resolution - Set DPI for high‑resolution output.
  • Color Mode - Choose between grayscale and color if the DICOM contains color data.

You can pass these options in the request payload. For example, to set quality:

request = ConvertDocumentRequest(
    file=dicom_bytes,
    format="jpg",
    options={"quality": 90}
)

All supported parameters are described in the API reference. Experiment with these settings to achieve the best balance between file size and diagnostic clarity, especially when working with large DICOM datasets.

Conclusion

DICOM to JPG conversion in Python becomes straightforward with the Aspose.HTML Cloud SDK for Python. The library handles the heavy lifting, offers multi‑threaded execution for high‑throughput environments, and provides granular control over image quality. Remember to secure a proper commercial license for production deployments; pricing details are available on the product page, and a temporary license can be obtained from the temporary license page. With the code and cURL examples above, you can integrate reliable DICOM to JPG conversion into any radiology application today.

FAQs

What file formats can the Aspose.HTML Cloud SDK convert besides DICOM?
The SDK supports a wide range of formats including PDF, PNG, BMP, GIF, TIFF, and SVG. Refer to the documentation for the full list.

Is it possible to run the conversion without installing any external tools?
Yes. The conversion is performed entirely in the cloud, so no local DICOM libraries or image processing tools are required.

How does multi‑threaded conversion improve performance?
By issuing several conversion requests in parallel, you can utilize multiple CPU cores on the Aspose servers, reducing overall processing time for large batches of DICOM files.

Can I use the SDK in a command‑line script?
Absolutely. The provided cURL examples demonstrate a command‑line workflow, and the Python script can be executed directly from a terminal or scheduled task.

Read More