Convert DICOM to PDF via Python

DICOM to PDF Python conversion. Programmers can use this example code to export DICOM to PDF within any .NET Framework, .NET Core, and PHP, VBScript, C++ via COM Interop.

Convert DICOM to PDF in Aspose.PDF for Python via .NET

How to convert DICOM to PDF? You can easily convert programmatically a document from DICOM to PDF format with a modern document-processing Python API. Use just a few lines of code to convert files with high quality. The Aspose.PDF library will allow any developer to easily solve the tasks of converting DICOM to PDF using Python.

For a more detailed description of the code snippet and other possible conversion formats, see the Documentation pages. Also, you can check the other conversions of formats, which are supported by our library.

With Aspose.PDF for Python via .NET library you can convert DICOM to PDF programmatically. PDF software from Aspose is ideal for individuals, small or large businesses. Since it is able to process a large amount of information, perform the conversion quickly and efficiently and protect your data. A peculiar feature from Aspose.PDF is an API for converting DICOM to PDF. The trait of this approach is that you only need to open the PyPI package manager, search for aspose-pdf, and install it without any special complex settings. To verify the benefits of the library, try using the conversion DICOM to PDF code snippet. You may also use the following command from the console or terminal:

Console

pip install aspose-pdf

How to Convert DICOM to PDF


Python developers can easily load & convert DICOM files to PDF in just a few lines of code.

  1. Define the paths to the input PDF file and output directory using path.join() function.
  2. Use pydicom library to load the DICOM image file from disk, specifying its file path using the dcmread() function. This step loads the entire DICOM image into memory.
  3. Extract the number of rows and columns from the loaded DICOM image using its Rows and Columns attributes, which represent the size of the image in pixels.
  4. Print a message to the console indicating the size of the loaded DICOM image in pixels, displaying its rows and columns.
  5. Create a new instance of the Document class from aspose.pdf library to create an empty PDF document. This step initializes the document with no pages or content.
  6. Use the add() method of the Document’s pages collection to add a new page to the document, which will be used as a container for the DICOM image.
  7. Create a new instance of the Image class from aspose.pdf library to represent the loaded DICOM image. This step sets up the image with its file type and content.
  8. Set the height, width, and margins (top, bottom, right, left) of the new page using various properties of the PageInfo object to match the size of the loaded DICOM image. This step ensures proper layout and positioning.
  9. Use the add() method of the Page’s paragraphs collection to add the loaded DICOM image as a paragraph to the new page, which will be rendered as an image in the PDF document. This step adds content to the document.
  10. Use the save() method of the Document object to write the completed PDF document to disk, specifying its file path using the outfile variable. This step finalizes the document and saves it to disk.

Here is an example that demonstrates how to convert DICOM to PDF in Python. You can follow these easy steps to convert your DICOM file to PDF format. First, upload your DICOM file and then simply save it as a PDF file. You can use fully qualified filenames for both DICOM reading and PDF writing. The output PDF content and formatting will be identical to the original DICOM document.

Example: Convert DICOM to PDF via Python

This sample code shows DICOM to PDF Python Conversion

Input file:

File not added

Output format:

PDF

Output file:

import aspose.pdf as apdf

from os import path
import pydicom

path_infile = path.join(self.data_dir, infile)
path_outfile = path.join(self.data_dir, outfile)

# Load the DICOM file
dicom_file = pydicom.dcmread(path_infile)

# Get the dimensions of the image
rows = dicom_file.Rows
columns = dicom_file.Columns

# Print the dimensions
print(f"DICOM image size: {rows} x {columns} pixels")

# Initialize new Document
document = apdf.Document()
page = document.pages.add()
image = apdf.Image()
image.file_type = apdf.ImageFileType.DICOM
image.file = path_infile

# Set page dimensions

page.page_info.height = rows
page.page_info.width = columns
page.page_info.margin.bottom = 0
page.page_info.margin.top = 0
page.page_info.margin.right = 0
page.page_info.margin.left = 0
page.paragraphs.add(image)

document.save(path_outfile)
print(infile + " converted into " + outfile)

Convert DICOM to PDF using Aspose.PDF for Python via .NET

Aspose.PDF for Python via .NET API supports most established PDF standards and PDF specifications. It allows developers to insert tables, graphs, images, hyperlinks, custom fonts - and more - into PDF documents. Moreover, it is also possible to compress PDF documents. Aspose.PDF for Python via .NET provides excellent security features to develop secure PDF documents. Some of the key features of Aspose.PDF for Python via .NET API include:

  • Ability to read & export PDF in multiple image formats including BMP, GIF, JPEG & PNG.
  • Set basic information (e.g. author, creator) of the PDF document.
  • Conversion Features: Convert PDF to Word, Excel, and PowerPoint. Convert PDF to Images formats. Convert PDF file to HTML format and vice versa. Convert PDF to EPUB, Text, XPS, etc.

You can find more information about Aspose.PDF for Python via .NET API on our documentation on how to use API.