Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created March 9, 2023 09:56

Aspose.Imaging for Python via .NET API allows to easy manipulate your dicom images or photos in your Python application or Web service.

You can:

  • Convert dicom image to raster formats;
  • Create dicom image;
  • Convert dicom image to vector formats;
  • Compress dicom images;
  • Perform crop, rotate, resize and other typical operations to dicom images.

Interested ?

You may go further at : https://products.aspose.com/imaging/python-net/

import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import DicomOptions
from aspose.imaging.xmp import XmpPacketWrapper
from aspose.imaging.xmp.schemas.dicom import DicomPackage
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.dicom")), DicomImage) as image:
xmp_packet_wrapper = XmpPacketWrapper()
dicom_package = DicomPackage()
dicom_package.set_equipment_institution("Test Institution")
dicom_package.set_equipment_manufacturer("Test Manufacturer")
dicom_package.set_patient_birth_date("19010101")
dicom_package.set_patient_id("010101")
dicom_package.set_patient_name("Test Name")
dicom_package.set_patient_sex("M")
dicom_package.set_series_date_time("19020202")
dicom_package.set_series_description("Test Series Description")
dicom_package.set_series_modality("Test Modality")
dicom_package.set_series_number("01")
dicom_package.set_study_date_time("19030303")
dicom_package.set_study_description("Test Study Description")
dicom_package.set_study_id("02")
dicom_package.set_study_physician("Test Physician")
xmp_packet_wrapper.add_package(dicom_package)
output_file = os.path.join(data_dir, "result.dcm")
dicom_options = DicomOptions()
dicom_options.xmp_data = xmp_packet_wrapper
image.save(output_file, dicom_options)
if delete_output:
os.remove(os.path.join(data_dir, "result.dcm"))
from aspose.imaging import Image
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import BmpOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.dicom"), "rb") as file_stream:
with DicomImage(file_stream) as image:
# Adjust the brightness and Create an instance of BmpOptions for the resultant image and Save the resultant image
image.adjust_brightness(50)
image.save(os.path.join(data_dir, "result.bmp"), BmpOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))
from aspose.imaging import Image
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import BmpOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.dicom"), "rb") as file_stream:
with DicomImage(file_stream) as image:
# Adjust contrast and Create an instance of BmpOptions for the resultant image and Save the resultant image
image.adjust_contrast(50)
image.save(os.path.join(data_dir, "result.bmp"), BmpOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))
from aspose.imaging import Image
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import BmpOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.dicom"), "rb") as file_stream:
with DicomImage(file_stream) as image:
# Adjust gamma and Create an instance of BmpOptions for the resultant image and Save the resultant image
image.adjust_gamma(5)
image.save(os.path.join(data_dir, "result.bmp"), BmpOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))
from aspose.imaging import Image
from aspose.imaging.imagefilters.filteroptions import MedianFilterOptions
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import BmpOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.dicom"), "rb") as file_stream:
with DicomImage(file_stream) as image:
# Apply filter and Create an instance of BmpOptions for the resultant image and Save the resultant image
image.filter(image.bounds, MedianFilterOptions(8))
image.save(os.path.join(data_dir, "result.bmp"), BmpOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))
import aspose.pycore as aspycore
from aspose.imaging import Image, RasterImage
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
data_dir = templates_folder
# Load an image in an instance of Image
with Image.load(os.path.join(data_dir, "template.dicom")) as image:
# Cast the image to RasterImage and Check if image is cached
raster_cached_image = aspycore.as_of(image, RasterImage)
if not raster_cached_image.is_cached:
# Cache image if not already cached
raster_cached_image.cache_data()
# Binarize image with predefined fixed threshold and Save the resultant image
raster_cached_image.binarize_fixed(100)
raster_cached_image.save(os.path.join(data_dir, "result.jpg"))
if delete_output:
os.remove(os.path.join(data_dir, "result.jpg"))
from aspose.imaging import Image
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import BmpOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.dicom"), "rb") as file_stream:
with DicomImage(file_stream) as image:
# Binarize image with Otsu Thresholding and Save the resultant image.
image.binarize_otsu()
image.save(os.path.join(data_dir, "result.bmp"), BmpOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))
from aspose.imaging import Image
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import BmpOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.dicom"), "rb") as file_stream:
with DicomImage(file_stream) as image:
# Crop dicom image and Save the resultant image.
image.crop(1, 1, 1, 1)
image.save(os.path.join(data_dir, "result.bmp"), BmpOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))
import aspose.pycore as aspycore
from aspose.imaging import Image
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import DicomOptions, JpegOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
# DICOM file format
# Digital Imaging and Communications in Medicine is the standard for the communication and management of medical imaging information. DICOM is most commonly used for storing and transmitting //medical images in medical devices such as scanners, servers, printers and picture archiving and communication systems (PACS). DICOM is used worldwide to store, exchange, and transmit //medical images.
# Various programs for Windows, macOS, and Linux can view DICOM files. DICOM uses the .DCM extension. These images can also be viewed online through certain web browsers. It is only //compatible using Chrome, Opera, Firefox, and Internet Explorer with the Google Chrome Frame extension installed.
# Why to use DICOM?
# DICOM provides a well-tested and widely accepted foundation for Medical Image Management. The advantages of using DICOM:
# Makes medical imaging information interoperable.
# Integrates image-acquisition devices, PACS, workstations, VNAs and printers from different manufacturers.
# Is actively developed and maintained to meet the evolving technologies and needs of medical imaging.
# Is free to download and use.
# Convert JPEG to DICOM
# The next code sample converts JPEG image to DICOM file format:
with Image.load(os.path.join(data_dir, "template.jpg")) as image:
image.save(os.path.join(data_dir, "result.dcm"), DicomOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.dcm"))
# Convert multipage images to DICOM
# DICOM format supports multipage images. You can convert GIF or TIFF images to DICOM in the same way as JPEG images:
with Image.load(os.path.join(data_dir, "template.gif")) as image:
image.save(os.path.join(data_dir, "result-2.dcm"), DicomOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result-2.dcm"))
# Export all DICOM pages to JPEG
# In case if you need to extract all the pages from DICOM file you can use the next code. It creates separate JPEG file for each DICOM page:
with aspycore.as_of(Image.load(os.path.join(data_dir, "template.dicom")), DicomImage) as image:
index = 1
for page in image.pages:
page.save(os.path.join(data_dir, f"Page {index}.jpeg"), JpegOptions())
if delete_output:
os.remove(os.path.join(data_dir, f"Page {index}.jpeg"))
index += 1
from aspose.imaging import Image, RotateFlipType
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import BmpOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.dicom"), "rb") as file_stream:
with DicomImage(file_stream) as image:
image.rotate_flip(RotateFlipType.ROTATE_180_FLIP_NONE)
image.save(os.path.join(data_dir, "result.bmp"), BmpOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))
from aspose.imaging import Image
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import BmpOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.dicom"), "rb") as file_stream:
with DicomImage(file_stream) as image:
# Grayscale dicom image and Save the resultant image.
image.grayscale()
image.save(os.path.join(data_dir, "result.bmp"), BmpOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))
from aspose.imaging import Image
from aspose.imaging.fileformats.dicom import DicomImage, ColorType, Compression, CompressionType
from aspose.imaging.imageoptions import DicomOptions, Jpeg2000Options, JpegOptions
from aspose.imaging.fileformats.jpeg import JpegCompressionMode, SampleRoundingMode
from aspose.imaging.fileformats.jpeg2000 import Jpeg2000Codec
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
# ### DICOM compression settings
# The property ***DicomOptions.Compression*** allows you to specify compression settings.
# For instance, ***CompressionType*** enumeration allows you to select compression algorithm:
# *None *, *Jpeg *, *Jpeg2000 * or * Rle *.The * None * option corresponds to uncompressed DICOM image.
# The following code shows how to use DICOM compression settings:
with Image.load(os.path.join(data_dir, "template.jpg")) as input_image:
compression = Compression()
compression.type = CompressionType.NONE
options = DicomOptions()
options.color_type = ColorType.RGB_24_BIT
options.compression = compression
input_image.save(os.path.join(data_dir, "result.dcm"), options)
if delete_output:
os.remove(os.path.join(data_dir, "result.dcm"))
# ### Using JPEG compression in DICOM image
# To use JPEG compression algorithm you should specify
# *CompressionType.Jpeg* enumeration value in ***Compression.Type*** property:
with Image.load(os.path.join(data_dir, "template.jpg")) as input_image:
compression = Compression()
compression.type = CompressionType.JPEG
options = DicomOptions()
options.color_type = ColorType.RGB_24_BIT
options.compression = compression
input_image.save(os.path.join(data_dir, "result.dcm"), options)
if delete_output:
os.remove(os.path.join(data_dir, "result.dcm"))
# You can tune JPEG compression algorithm using ***Compression.Jpeg * **property.For instance,
# you can specify the *CompressionType*, *SampleRoundingMode* and *Quality*:
with Image.load(os.path.join(data_dir, "template.jpg")) as input_image:
jpeg_options = JpegOptions()
jpeg_options.compression_type = JpegCompressionMode.BASELINE
jpeg_options.sample_rounding_mode = SampleRoundingMode.TRUNCATE
jpeg_options.quality = 50
compression = Compression()
compression.type = CompressionType.JPEG
compression.jpeg = jpeg_options
options = DicomOptions()
options.color_type = ColorType.RGB_24_BIT
options.compression = compression
input_image.save(os.path.join(data_dir, "result.dcm"), options)
if delete_output:
os.remove(os.path.join(data_dir, "result.dcm"))
# ### Using JPEG 2000 compression in DICOM image
# To use JPEG 2000 compression you need to use *CompressionType.Jpeg2000* enumeration value and
# ***Jpeg2000Options*** class for algorithm settings. The following code demonstrates how to specify
# JPEG 2000 * Codec * and * Irreversible * properties:
with Image.load(os.path.join(data_dir, "template.jpg")) as input_image:
j2k_options = Jpeg2000Options()
j2k_options.codec = Jpeg2000Codec.JP2
j2k_options.irreversible = False
compression = Compression()
compression.type = CompressionType.JPEG2000
compression.jpeg2000 = j2k_options
options = DicomOptions()
options.color_type = ColorType.RGB_24_BIT
options.compression = compression
input_image.save(os.path.join(data_dir, "result.dcm"), options)
if delete_output:
os.remove(os.path.join(data_dir, "result.dcm"))
# ### Using RLE compression in DICOM image
# For this compression type you need to use *CompressionType.Rle* enumeration value.The RLE compression
# algorithm doesn't have additional settings. The following code shows how you can use RLE compression
# algorithm in DICOM image:
with Image.load(os.path.join(data_dir, "template.jpg")) as input_image:
compression = Compression()
compression.type = CompressionType.RLE
options = DicomOptions()
options.color_type = ColorType.RGB_24_BIT
options.compression = compression
input_image.save(os.path.join(data_dir, "result.dcm"), options)
if delete_output:
os.remove(os.path.join(data_dir, "result.dcm"))
# ### How to change Color Type in DICOM compression
# The property ***DicomOptions.ColorType*** allows you to change color type in DICOM compression.
# There are several supported color types: *Grayscale8Bit *, *Grayscale16Bit * and * Rgb24Bit *.Use the following code in order to change the color type:
with Image.load(os.path.join(data_dir, "template.jpg")) as input_image:
options = DicomOptions()
options.color_type = ColorType.GRAYSCALE_8_BIT
input_image.save(os.path.join(data_dir, "result.dcm"), options)
if delete_output:
os.remove(os.path.join(data_dir, "result.dcm"))
from aspose.imaging import Image, LoadOptions
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import DicomOptions
from aspose.imaging.sources import FileCreateSource
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
# Example 1. Setting a memory limit of 50 megabytes for operations on the created Dicom image
with DicomOptions() as image_options:
image_options.source = FileCreateSource(os.path.join(data_dir, "created.dcm"), False)
image_options.buffer_size_hint = 50
with Image.create(image_options, 1000, 1000) as image:
# Do something with the created image
image.save()
if delete_output:
os.remove(os.path.join(data_dir, "created.dcm"))
# Example 2. Setting a memory limit of 20 megabytes for operations on the loaded Dicom image
load_options = LoadOptions()
load_options.buffer_size_hint = 20
with Image.load(os.path.join(data_dir, "template.dcm"), load_options) as image:
pass
# Example 3. Settings a memory limit of 30 megabytes for export-to-dicom operation
load_options = LoadOptions()
load_options.buffer_size_hint = 30
with Image.load(os.path.join(data_dir, "template.png"), load_options) as image:
image.save(os.path.join(data_dir, "result.dcm"), DicomOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.dcm"))
from aspose.imaging import Image, ResizeType
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import BmpOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.dicom"), "rb") as file_stream:
with DicomImage(file_stream) as image:
# Resize dicom image and Save the resultant image.
image.resize_height_proportionally(100, ResizeType.ADAPTIVE_RESAMPLE)
image.resize_width_proportionally(150, ResizeType.ADAPTIVE_RESAMPLE)
image.save(os.path.join(data_dir, "result.bmp"), BmpOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))
from aspose.imaging import Image
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import BmpOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.dicom"), "rb") as file_stream:
with DicomImage(file_stream) as image:
# Rotate dicom image and Save the resultant image.
image.rotate(10)
image.save(os.path.join(data_dir, "result.bmp"), BmpOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))
from aspose.imaging import Image, ResizeType
from aspose.imaging.fileformats.dicom import DicomImage
from aspose.imaging.imageoptions import BmpOptions
import os
if 'TEMPLATE_DIR' in os.environ:
templates_folder = os.environ['TEMPLATE_DIR']
else:
templates_folder = r"C:\Users\USER\Downloads\templates"
delete_output = 'SAVE_OUTPUT' not in os.environ
data_dir = templates_folder
with open(os.path.join(data_dir, "template.dicom"), "rb") as file_stream:
with DicomImage(file_stream) as image:
# Resize dicom image and Save the resultant image.
image.resize(200, 200)
image.save(os.path.join(data_dir, "result.bmp"), BmpOptions())
if delete_output:
os.remove(os.path.join(data_dir, "result.bmp"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment