{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":10338,"databundleVersionId":862042}],"dockerImageVersionId":31286,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session\n\n!kaggle competitions download -c rsna-pneumonia-detection-challenge\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install pydicom opencv-python","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport cv2\nimport pydicom\nimport numpy as np\nfrom tqdm import tqdm","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dicom_dir = \"/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images\"\noutput_dir = \"/kaggle/working/train_jpg\"\n\nos.makedirs(output_dir, exist_ok=True)","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def dicom_to_jpg(dicom_path, save_path, size=224):\n\n    dcm = pydicom.dcmread(dicom_path)\n    image = dcm.pixel_array.astype(np.float32)\n\n    # Fix inverted X-rays\n    if dcm.PhotometricInterpretation == \"MONOCHROME1\":\n        image = np.max(image) - image\n\n    # Normalize pixel values\n    image -= np.min(image)\n    image /= np.max(image)\n    image *= 255.0\n\n    image = image.astype(np.uint8)\n\n    # Resize for Vision Transformer\n    image = cv2.resize(image, (size, size))\n\n    cv2.imwrite(save_path, image)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-25T09:10:21.956331Z","iopub.execute_input":"2026-02-25T09:10:21.957203Z","iopub.status.idle":"2026-02-25T09:10:21.962972Z","shell.execute_reply.started":"2026-02-25T09:10:21.957172Z","shell.execute_reply":"2026-02-25T09:10:21.961977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"files = os.listdir(dicom_dir)\n\nfor file in tqdm(files):\n    if file.endswith(\".dcm\"):\n        input_path = os.path.join(dicom_dir, file)\n        output_path = os.path.join(output_dir, file.replace(\".dcm\", \".jpg\"))\n\n        dicom_to_jpg(input_path, output_path)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-25T09:10:43.718296Z","iopub.execute_input":"2026-02-25T09:10:43.719335Z","iopub.status.idle":"2026-02-25T09:17:29.742422Z","shell.execute_reply.started":"2026-02-25T09:10:43.719301Z","shell.execute_reply":"2026-02-25T09:17:29.741431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(os.listdir(output_dir))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-25T09:17:54.342859Z","iopub.execute_input":"2026-02-25T09:17:54.343446Z","iopub.status.idle":"2026-02-25T09:17:54.372134Z","shell.execute_reply.started":"2026-02-25T09:17:54.343416Z","shell.execute_reply":"2026-02-25T09:17:54.371231Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nimg = cv2.imread(os.path.join(output_dir, os.listdir(output_dir)[0]), 0)\nplt.imshow(img, cmap=\"gray\")\nplt.axis(\"off\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-02-25T09:18:10.128403Z","iopub.execute_input":"2026-02-25T09:18:10.129556Z","iopub.status.idle":"2026-02-25T09:18:10.296691Z","shell.execute_reply.started":"2026-02-25T09:18:10.129490Z","shell.execute_reply":"2026-02-25T09:18:10.295583Z"}},"outputs":[],"execution_count":null}]}