<jupyter_start><jupyter_text>NoteTo safe Following sections are example methods to perform DICOM to JPEG2K conversion, please always double check if the generated JPEG files are lossless.Currently you're receiving errors running following code on Kaggle Kernels that you're not having issues with locally. The kernels libraries may not match, and we are working this issue. Potential problem is mis-configured openjpeg package. Any thoughts and comments are appreciated.<jupyter_code># Convert to JPEG2K via Pillow
# https://github.com/python-pillow/Pillow
 
import os
import pydicom
import glob
from PIL import Image

inputdir = '../input/sample images/'
outdir = './'

test_list = [os.path.basename(x) for x in glob.glob(inputdir + './*.dcm')]
for f in test_list:  
    ds = pydicom.read_file( inputdir + f) # read dicom image
    img = ds.pixel_array # get image array
    img_mem = Image.fromarray(img) # Creates an image memory from an object exporting the array interface
    
#   There is an exception in Kaggle kernel about "encoder jpeg2k not available", please test following code on your local workstation
#   img_mem.save(outdir + f.replace('.dcm','.jp2'))
# Convert to JPEG2K via imageio
# https://github.com/imageio/imageio

import os
import pydicom
import glob
import imageio

inputdir = '../input/sample images/'
outdir = './'

test_list = [os.path.basename(x) for x in glob.glob(inputdir + './*.dcm')]
for f in test_list:  
    ds = pydicom.read_file(inputdir + f) # read dicom image
    img = ds.pixel_array # get image array
    
#   There is an exception in Kaggle kernel about "encoder jpeg2k not available", please test following code on your local workstation
#   imageio.imwrite(outdir + f.replace('.dcm','.jp2'), img)<jupyter_output><empty_output><jupyter_text>Other options<jupyter_code># DCMTK 
DCMTK (https://dicom.offis.de/dcmtk.php.en) also provides DICOM conversion to JPEG 2000

Install guide can be found at Quick installation section (https://dicom.offis.de/dcmtk.php.en). 

To perform DCMTK, please use executable binary software "dcmj2pnm" and run (e.g. Linux user)

> dcmj2pnm /PATH_TO_SAMPLE_DICOM/sample.dcm /PATH_TO_SAVE_JPEG2K/sample.jpg

The create "*.jpg" file should be value lossless. Please do double check.  
# Convert DICOM to PNG via openCV
import cv2
import os
import pydicom
import glob

inputdir = './'
outdir = './'
#os.mkdir(outdir)

test_list = [os.path.basename(x) for x in glob.glob(inputdir + './*.dcm')]

for f in test_list:   
    ds = pydicom.read_file(inputdir + f) # read dicom image
    img = ds.pixel_array # get image array
    cv2.imwrite(outdir + f.replace('.dcm','.png'),img) # write png image
# Convert to PNG via PIL 
# https://github.com/python-pillow/Pillow
import os
import pydicom
import glob
from PIL import Image

inputdir = '../input/sample images/'
outdir = './'

test_list = [os.path.basename(x) for x in glob.glob(inputdir + './*.dcm')]
#glob.glob(inputdir + './*.dcm')
for f in test_list:   
    ds = pydicom.read_file( inputdir + f) # read dicom image
    img = ds.pixel_array # get image array
    img_mem = Image.fromarray(img) # Creates an image memory from an object exporting the array interface
    img_mem.save(outdir + f.replace('.dcm','.png'))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10658499234303216, 'fraction_numerical': 0.004900459418070444, 'mean_word_length': 4.04791344667697, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 10, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '612542', 'license': 'MIT', 'n_tokens_mistral': 1111, 'n_tokens_neox': 1088, 'n_words': 359, 'path': 'DICOM to JPEG Conversion Kernel.ipynb', 'repo_name': 'KunalGehlot/Pneumothorax-detection', 'size': '3265'}