Package: pylibjpeg
pylibjpegA Python 3.8+ framework for decoding JPEG images and decoding/encoding RLE datasets, with a focus on providing support forpydicom.InstallationInstalling the current releasepip install pylibjpegInstalling extra requirementsThe package can be installed with extra requirements to enable support for JPEG (withlibjpeg), JPEG 2000 (withopenjpeg) and Run-Length Encoding (RLE) (withrle), respectively:pip install pylibjpeg[libjpeg,openjpeg,rle]Or alternatively with justall:pip install pylibjpeg[all]Installing the development versionMake sureGitis installed, thengitclonehttps://github.com/pydicom/pylibjpeg
python-mpipinstallpylibjpegPluginsOne or more plugins are required beforepylibjpegis able to handle JPEG images or RLE datasets. To handle a given format or DICOM Transfer Syntax
you first have to install the corresponding package:Supported Image FormatsFormatDecode?Encode?PluginLicenseBased onJPEG, JPEG-LS and JPEG XTYesNopylibjpeg-libjpegGPLv3libjpegJPEG 2000YesNopylibjpeg-openjpegMITopenjpegRLE Lossless (PackBits)YesYespylibjpeg-rleMIT-Supported DICOM Transfer SyntaxesUIDDescriptionPlugin1.2.840.10008.1.2.4.50JPEG Baseline (Process 1)pylibjpeg-libjpeg1.2.840.10008.1.2.4.51JPEG Extended (Process 2 and 4)pylibjpeg-libjpeg1.2.840.10008.1.2.4.57JPEG Lossless, Non-Hierarchical (Process 14)pylibjpeg-libjpeg1.2.840.10008.1.2.4.70JPEG Lossless, Non-Hierarchical, First-Order Prediction(Process 14, Selection Value 1)pylibjpeg-libjpeg1.2.840.10008.1.2.4.80JPEG-LS Losslesspylibjpeg-libjpeg1.2.840.10008.1.2.4.81JPEG-LS Lossy (Near-Lossless) Image Compressionpylibjpeg-libjpeg1.2.840.10008.1.2.4.90JPEG 2000 Image Compression (Lossless Only)pylibjpeg-openjpeg1.2.840.10008.1.2.4.91JPEG 2000 Image Compressionpylibjpeg-openjpeg1.2.840.10008.1.2.4.201High-Throughput JPEG 2000 Image Compression (Lossless Only)pylibjpeg-openjpeg1.2.840.10008.1.2.4.202High-Throughput JPEG 2000 with RPCL Options Image Compression (Lossless Only)pylibjpeg-openjpeg1.2.840.10008.1.2.4.203High-Throughput JPEG 2000 Image Compressionpylibjpeg-openjpeg1.2.840.10008.1.2.5RLE Losslesspylibjpeg-rleIf you're not sure what the dataset'sTransfer Syntax UIDis, it can be
determined with:>>>frompydicomimportdcmread>>>ds=dcmread('path/to/dicom_file')>>>ds.file_meta.TransferSyntaxUID.nameUsageDecodingWith pydicomAssuming you havepydicomv2.1+ and suitable plugins installed:frompydicomimportdcmreadfrompydicom.dataimportget_testdata_file# With the pylibjpeg-libjpeg pluginds=dcmread(get_testdata_file('JPEG-LL.dcm'))jpg_arr=ds.pixel_array# With the pylibjpeg-openjpeg pluginds=dcmread(get_testdata_file('JPEG2000.dcm'))j2k_arr=ds.pixel_array# With the pylibjpeg-rle plugin and pydicom v2.2+ds=dcmread(get_testdata_file('OBXXXX1A_rle.dcm'))# pydicom defaults to the numpy handler for RLE so need# to explicitly specify the use of pylibjpegds.decompress("pylibjpeg")rle_arr=ds.pixel_arrayStandalone JPEG decodingYou can also just usepylibjpegto decode JPEG images to anumpy ndarray, provided you have a suitable plugin installed:frompylibjpegimportdecode# Can decode using the path to a JPG file as str or path-likearr=decode('filename.jpg')# Or a file-like...withopen('filename.jpg','rb')asf:arr=decode(f)# Or bytes...withopen('filename.jpg','rb')asf:arr=decode(f.read())EncodingWith pydicomAssuming you havepydicomv2.2+ and suitable plugins installed:frompydicomimportdcmreadfrompydicom.dataimportget_testdata_filefrompydicom.uidimportRLELosslessds=dcmread(get_testdata_file("CT_small.dcm"))# Encode in-place using RLE Lossless and update the dataset# Updates the Pixel Data, Transfer Syntax UID and Planar Configurationds.compress(uid)# Save compressedds.save_as("CT_small_rle.dcm")