{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"# https://pypng.readthedocs.io/en/latest/ca.html\n!pip install pypng","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport pydicom\nimport png\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nfrom skimage.transform import resize\nimport tensorflow as tf\nfrom tqdm.notebook import tqdm\nimport warnings\nwarnings.filterwarnings('ignore') ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_dir = \"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train/\"\ntest_dir = \"/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/test/\"\n\ntrain_images = os.listdir(train_dir)\ntest_images = os.listdir(test_dir)\n\ntrain_path_image = [train_dir + image for image in train_images]\ntest_path_image = [test_dir + image for image in test_images]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filetrain = './trainpng/' \n\nif not os.path.exists(os.path.dirname(filetrain)):\n    os.makedirs(filetrain)\n    \nfiletest = './testpng/' \n\nif not os.path.exists(os.path.dirname(filetest)):\n    os.makedirs(filetest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"filetrain + train_path_image[0].split('/')[-1].replace(\".dicom\", \".png\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def convert_to_png(full_path_image, filename):\n    print(filename)\n    for dicomimage in tqdm(full_path_image):\n        ds = pydicom.dcmread(dicomimage)\n        img = apply_voi_lut(ds.pixel_array, ds)\n        img = resize(img, (1024, 1024), anti_aliasing=True)\n        \n        if ds.PhotometricInterpretation == \"MONOCHROME1\":\n            img = np.amax(img) - img\n            \n        img = (((img - np.min(img))/np.max(img))*255.0).astype(np.uint8) \n        \n        with open(filename + dicomimage.split('/')[-1].replace(\".dicom\", \".png\"), \"wb\") as fn:\n            \n            w = png.Writer(1024, 1024, greyscale=True)\n            w.write(fn, img)\n       ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"convert_to_png(train_path_image[:100], filetrain)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"convert_to_png(test_path_image[:100], filetest)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!tar -zcf train.tar.gz './trainpng'\n!tar -zcf test.tar.gz './testpng'","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}