การแปลงกราฟิกการแพทย์แบบเคลื่อนไหวเป็นรูปแบบที่ระบบรังสีวิทยาเข้าใจอาจเป็นคอขวดสำหรับนักพัฒนา. Aspose.Slides Cloud SDK for Python มีไลบรารีที่ทรงพลังซึ่งทำให้งานนี้ง่ายขึ้น. ในคู่มือนี้คุณจะเห็นตัวอย่างการแปลงจาก GIF ไปเป็น DICOM อย่างครบถ้วนใน Python, เรียนรู้วิธีตั้งค่าการรับรองความถูกต้อง, จัดการการประมวลผลแบบแบตช์, และสำรวจการเรียก cURL ที่เทียบเท่าสำหรับการรวม REST.

วิธีแปลงภาพ GIF เป็นไฟล์ DICOM ด้วย Python - ขั้นตอนโดยละเอียด

  1. กำหนดการรับรองความถูกต้องและเริ่มต้น SlidesApi: ตั้งค่าข้อมูลประจำตัวของไคลเอนต์ของคุณและสร้างอินสแตนซ์ SlidesApi.
from asposeslidescloud import SlidesApi, Configuration

config = Configuration()
config.api_key['client_id'] = "YOUR_CLIENT_ID"
config.api_key['client_secret'] = "YOUR_CLIENT_SECRET"
slides_api = SlidesApi(configuration=config)

ขั้นตอนนี้เตรียมสภาพแวดล้อมสำหรับตัวอย่างการแปลง GIF เป็น DICOM ใน Python.

  1. กำหนดโฟลเดอร์อินพุตและเอาต์พุต: สร้างโฟลเดอร์สำหรับ GIF ต้นฉบับและไฟล์ DICOM ที่สร้างขึ้น.
import os

INPUT_FOLDER = "gif_inputs"
OUTPUT_FOLDER = "dicom_outputs"
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
  1. สร้างฟังก์ชันการแปลง: ใช้ convert_local เพื่อแปลง GIF เป็น DICOM โดยส่งเมตาดาต้าแบบเลือกเพิ่มเติม
def convert_gif_to_dicom(gif_path: str, dicom_path: str):
    with open(gif_path, "rb") as gif_file:
        result_bytes = slides_api.convert_local(
            file=gif_file,
            format="dicom",
            patientName="John Doe",
            patientId="123456",
            studyDescription="Sample GIF to DICOM conversion"
        )
    with open(dicom_path, "wb") as out_file:
        out_file.write(result_bytes)
  1. แปลงไฟล์ GIF ทั้งหมดเป็นชุด: สแกนโฟลเดอร์อินพุตและเรียกฟังก์ชันการแปลงสำหรับแต่ละไฟล์.
import glob

def batch_convert_gif_folder(input_folder: str, output_folder: str):
    gif_files = glob.glob(os.path.join(input_folder, "*.gif"))
    for gif_file in gif_files:
        base_name = os.path.splitext(os.path.basename(gif_file))[0]
        dicom_file = os.path.join(output_folder, f"{base_name}.dcm")
        convert_gif_to_dicom(gif_file, dicom_file)
  1. เรียกใช้สคริปต์: เรียกใช้การแปลงเป็นชุดเมื่อโมดูลถูกเรียกใช้งานโดยตรง.
if __name__ == "__main__":
    batch_convert_gif_folder(INPUT_FOLDER, OUTPUT_FOLDER)

สคริปต์นี้ทำให้ตัวอย่างการแปลง GIF เป็น DICOM ใน Python เสร็จสมบูรณ์และให้ไฟล์ DICOM ที่พร้อมใช้งานแก่คุณ.

ตัวอย่างโค้ดเต็ม: การแปลง GIF เป็น DICOM โดยใช้ Aspose.Slides Cloud SDK

โค้ดต่อไปนี้แสดงการดำเนินการเต็มรูปแบบของตัวอย่างการแปลง GIF เป็น DICOM ด้วย Python.

import os
import glob
from asposeslidescloud import SlidesApi, ApiException, Configuration

# -------------------- Setup Aspose.Slides Cloud SDK and Authentication --------------------
# Replace with your actual Aspose Cloud credentials
CLIENT_ID = "YOUR_CLIENT_ID"
CLIENT_SECRET = "YOUR_CLIENT_SECRET"

config = Configuration()
config.api_key['client_id'] = CLIENT_ID
config.api_key['client_secret'] = CLIENT_SECRET
# Optional: set the base URL if you use a different region
# config.host = "https://api.aspose.cloud"

slides_api = SlidesApi(configuration=config)

# -------------------- Prepare GIF Input and Define DICOM Parameters --------------------
INPUT_FOLDER = "gif_inputs"
OUTPUT_FOLDER = "dicom_outputs"
os.makedirs(OUTPUT_FOLDER, exist_ok=True)

# Example DICOM metadata (if the API supports passing them as query parameters)
# Here we just illustrate the idea; actual parameter names may differ.
dicom_params = {
    "patientName": "John Doe",
    "patientId": "123456",
    "studyDescription": "Sample GIF to DICOM conversion"
}

# -------------------- Write Python Script for GIF to DICOM Conversion Example --------------------
def convert_gif_to_dicom(gif_path: str, dicom_path: str):
    """
    Converts a single GIF file to DICOM format using Aspose.Slides Cloud SDK.
    """
    try:
        # Read the GIF file into memory
        with open(gif_path, "rb") as gif_file:
            # The SDK method `convert_local` returns the converted file bytes.
            # Pass the desired output format ("dicom") and any additional parameters.
            result_bytes = slides_api.convert_local(
                file=gif_file,
                format="dicom",
                # The following dictionary is illustrative; adjust according to SDK docs.
                **dicom_params
            )
        # Write the resulting DICOM bytes to the output file
        with open(dicom_path, "wb") as out_file:
            out_file.write(result_bytes)
        print(f"Converted '{gif_path}' → '{dicom_path}'")
    except ApiException as e:
        print(f"API error while converting '{gif_path}': {e}")
    except Exception as ex:
        print(f"Unexpected error while converting '{gif_path}': {ex}")

# -------------------- Batch Convert GIF Files to DICOM in Python --------------------
def batch_convert_gif_folder(input_folder: str, output_folder: str):
    """
    Scans the input folder for *.gif files and converts each to DICOM,
    storing results in the output folder.
    """
    gif_files = glob.glob(os.path.join(input_folder, "*.gif"))
    if not gif_files:
        print(f"No GIF files found in '{input_folder}'.")
        return

for gif_file in gif_files:
        base_name = os.path.splitext(os.path.basename(gif_file))[0]
        dicom_file = os.path.join(output_folder, f"{base_name}.dcm")
        convert_gif_to_dicom(gif_file, dicom_file)

# -------------------- Execute Batch Conversion --------------------
if __name__ == "__main__":
    batch_convert_gif_folder(INPUT_FOLDER, OUTPUT_FOLDER)

หมายเหตุ: ตัวอย่างโค้ดนี้แสดงการทำงานหลัก ก่อนนำไปใช้ในโครงการของคุณ โปรดตรวจสอบให้แน่ใจว่าได้อัปเดตเส้นทางไฟล์และค่าการกำหนดค่าให้ตรงกับสภาพแวดล้อมจริงของคุณ ตรวจสอบว่าขึ้นตอนการพึ่งพาที่จำเป็นทั้งหมดได้ติดตั้งอย่างถูกต้อง และทดสอบอย่างละเอียดในสภาพแวดล้อมการพัฒนา หากพบปัญหาใด ๆ โปรดอ้างอิงที่ เอกสารอย่างเป็นทางการ หรือ ติดต่อทีม ฝ่ายสนับสนุน เพื่อขอความช่วยเหลือ.

การใช้ cURL เพื่อเรียกใช้ Endpoint การแปลงของ Aspose.Slides Cloud

ด้านล่างเป็นการเรียก REST ที่ทำการแปลง GIF เป็น DICOM ตัวอย่างเดียวกันใน Python ผ่าน Aspose.Slides Cloud API.

  1. รับโทเค็นการเข้าถึง (แทนที่ตัวแปรด้วยข้อมูลประจำตัวของคุณ).
curl -X POST "https://api.aspose.cloud/connect/token" \
     -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"
  1. อัปโหลดไฟล์ GIF ไปยังที่เก็บชั่วคราว.
curl -X PUT "https://api.aspose.cloud/v3.0/slides/storage/file/gif_inputs/sample.gif" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -H "Content-Type: application/octet-stream" \
     --data-binary "@sample.gif"
  1. Request conversion to DICOM พร้อมส่งเมตาดาต้าเพิ่มเติม.
curl -X POST "https://api.aspose.cloud/v3.0/slides/gif_inputs/sample.gif/convert/dicom?patientName=John%20Doe&patientId=123456&studyDescription=Sample%20GIF%20to%20DICOM%20conversion" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -o "dicom_outputs/sample.dcm"
  1. ดาวน์โหลดไฟล์ DICOM ที่ได้ (หากคุณบันทึกไว้ที่อื่น).
curl -X GET "https://api.aspose.cloud/v3.0/slides/storage/file/dicom_outputs/sample.dcm" \
     -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
     -o "sample.dcm"

สำหรับรายละเอียดเพิ่มเติมเกี่ยวกับพารามิเตอร์ของคำขอ ดูที่ อ้างอิง API.

การติดตั้งและกำหนดค่า Aspose.Slides Cloud SDK for Python

SDK ถูกแจกจ่ายผ่าน PyPI. ติดตั้งด้วย pip และตรวจสอบเส้นทางการติดตั้ง.

pip install asposeslidescloud

หลังจากการติดตั้ง ให้สร้างไฟล์กำหนดค่า หรือกำหนดตัวแปรสภาพแวดล้อมด้วย CLIENT_ID และ CLIENT_SECRET ของคุณ ส่วนโค้ดตัวอย่างในส่วน Steps แสดงวิธีสร้างอ็อบเจกต์ Configuration

การตั้งค่าการแปลง: ตัวเลือกและพารามิเตอร์

คุณสามารถปรับแต่งผลลัพธ์ DICOM ได้โดยการปรับพจนานุกรม dicom_params ฟิลด์ทั่วไปรวมถึงข้อมูลผู้ป่วยและคำอธิบายการศึกษา.

dicom_params = {
    "patientName": "John Doe",
    "patientId": "123456",
    "studyDescription": "Sample GIF to DICOM conversion"
}

พารามิเตอร์เพิ่มเติมเช่น ความละเอียดของภาพหรือการบีบอัดสามารถส่งได้หาก API รองรับ; ดูที่ อ้างอิง API สำหรับรายการทั้งหมด.

คำถามที่พบบ่อย

  • ฉันสามารถแปลง GIF เดียวเป็น DICOM ด้วย SDK ได้หรือไม่?
    ใช่, คุณสามารถเรียกใช้เมธอด convert_local สำหรับไฟล์ GIF แต่ละไฟล์ได้ ดูเอกสารของ Aspose.Slides Cloud SDK for Python สำหรับรายละเอียดเพิ่มเติม.

  • ฉันจะทำการแปลงไฟล์ GIF เป็น DICOM เป็นชุดใน Python อย่างไร?
    ใช้ฟังก์ชัน batch_convert_gif_folder มันสแกนโฟลเดอร์เพื่อค้นหาไฟล์ *.GIF และแปลงแต่ละไฟล์ ซึ่งเหมาะสำหรับชุดภาพขนาดใหญ่

  • มีวิธีแปลง DICOM กลับเป็น JPEG หรือ PNG หรือไม่?
    SDK มุ่งเน้นการแปลงเป็น DICOM แต่คุณสามารถใช้ Aspose.Imaging Cloud SDK for .NET หรือผลิตภัณฑ์ที่รองรับอื่น ๆ เพื่อดึงภาพจากไฟล์ DICOM ได้

  • ตัวเลือกการให้สิทธิ์ใช้งานมีอะไรบ้างและฉันจะขอรับใบอนุญาตชั่วคราวได้อย่างไร?
    จำเป็นต้องมีใบอนุญาตเชิงพาณิชย์สำหรับการใช้งานในสภาพแวดล้อมการผลิต คุณสามารถรับใบอนุญาตชั่วคราวเพื่อการประเมินได้จาก temporary license page.

บทความที่เกี่ยวข้อง