{"library":"pylibjpeg","title":"pylibjpeg: JPEG and DICOM RLE Decoder Framework","description":"pylibjpeg is a Python framework for decoding JPEG and decoding/encoding DICOM RLE data, primarily designed to support pydicom. It provides a flexible handler system that allows different backend implementations for JPEG and RLE to be used. The current version is 2.1.0, and it follows an infrequent release cadence, often tied to significant architectural changes or `pydicom`'s decoding needs.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install pylibjpeg","pip install pylibjpeg-libjpeg","pip install pylibjpeg-rle"],"cli":null},"imports":["import pylibjpeg; pylibjpeg.add_handler(...)","import pylibjpeg_libjpeg","import pylibjpeg_rle"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import pydicom\nimport pylibjpeg\nimport os\n\n# Ensure you have installed a backend, e.g., 'pip install pylibjpeg-libjpeg'\n# and optionally 'pip install pylibjpeg-rle' for RLE support.\n\ntry:\n    import pylibjpeg_libjpeg\n    # Register the JPEG handler. For pydicom, registering the top-level package\n    # automatically adds all handlers within it.\n    pylibjpeg.add_handler(pylibjpeg_libjpeg)\n    print(\"pylibjpeg_libjpeg handler successfully added.\")\nexcept ImportError:\n    print(\"Warning: pylibjpeg-libjpeg is not installed. JPEG decoding will not be available.\")\n\ntry:\n    import pylibjpeg_rle\n    pylibjpeg.add_handler(pylibjpeg_rle)\n    print(\"pylibjpeg_rle handler successfully added.\")\nexcept ImportError:\n    print(\"Warning: pylibjpeg-rle is not installed. RLE decoding/encoding will not be available.\")\n\nprint(f\"Current active pylibjpeg handlers: {pylibjpeg.get_handlers()}\")\n\n# Example: Decode a DICOM file using pydicom (requires a valid DICOM file)\n# Set DICOM_FILE_PATH environment variable for testing, e.g., an image with JPEG/RLE compression\ndicom_file_path = os.environ.get('DICOM_FILE_PATH', 'path/to/your/compressed_dicom.dcm')\n\nif os.path.exists(dicom_file_path):\n    try:\n        ds = pydicom.dcmread(dicom_file_path)\n        if hasattr(ds, 'pixel_array'):\n            pixel_array = ds.pixel_array\n            print(f\"Successfully decoded pixel data from '{dicom_file_path}' with shape: {pixel_array.shape}\")\n        else:\n            print(f\"DICOM file '{dicom_file_path}' does not contain pixel data or could not be decoded by available handlers.\")\n    except Exception as e:\n        print(f\"Error reading DICOM file or decoding pixel data: {e}\")\nelse:\n    print(f\"Skipping DICOM decode example: DICOM_FILE_PATH '{dicom_file_path}' not found or not set.\")\n","lang":"python","description":"This quickstart demonstrates how to install `pylibjpeg` and its necessary backend handlers, then registers them with the `pydicom` library to enable decoding of compressed DICOM pixel data (JPEG, RLE). You must install the `pylibjpeg-libjpeg` and/or `pylibjpeg-rle` packages separately to enable their respective decoding capabilities. The `DICOM_FILE_PATH` environment variable can be set to point to a test DICOM file.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}