pylibjpeg
A Python 3.8+ framework for decoding JPEG images and decoding/encoding RLE datasets, with a focus on providing support for pydicom.
Installation
Installing the current release
pip install pylibjpeg
The package can be installed with extra requirements to enable support for JPEG (with libjpeg
), JPEG 2000 (with openjpeg
) and Run-Length Encoding (RLE) (with rle
), respectively:
pip install pylibjpeg[libjpeg,openjpeg,rle]
Or alternatively with just all
:
pip install pylibjpeg[all]
Installing the development version
Make sure Git is installed, then
git clone https://github.com/pydicom/pylibjpeg
python -m pip install pylibjpeg
Plugins
One or more plugins are required before pylibjpeg is 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 Formats
Supported DICOM Transfer Syntaxes
If you're not sure what the dataset's Transfer Syntax UID is, it can be
determined with:
>>> from pydicom import dcmread
>>> ds = dcmread('path/to/dicom_file')
>>> ds.file_meta.TransferSyntaxUID.name
Usage
Decoding
With pydicom
Assuming you have pydicom v2.1+ and suitable plugins installed:
from pydicom import dcmread
from pydicom.data import get_testdata_file
ds = dcmread(get_testdata_file('JPEG-LL.dcm'))
jpg_arr = ds.pixel_array
ds = dcmread(get_testdata_file('JPEG2000.dcm'))
j2k_arr = ds.pixel_array
ds = dcmread(get_testdata_file('OBXXXX1A_rle.dcm'))
ds.decompress("pylibjpeg")
rle_arr = ds.pixel_array
Standalone JPEG decoding
You can also just use pylibjpeg to decode JPEG images to a numpy ndarray, provided you have a suitable plugin installed:
from pylibjpeg import decode
arr = decode('filename.jpg')
with open('filename.jpg', 'rb') as f:
arr = decode(f)
with open('filename.jpg', 'rb') as f:
arr = decode(f.read())
Encoding
With pydicom
Assuming you have pydicom v2.2+ and suitable plugins installed:
from pydicom import dcmread
from pydicom.data import get_testdata_file
from pydicom.uid import RLELossless
ds = dcmread(get_testdata_file("CT_small.dcm"))
ds.compress(RLELossless)
ds.save_as("CT_small_rle.dcm")