|
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"))
|