{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nfrom pathlib import Path\nimport os\nimport PIL\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\n\nPATH = Path(\"../input\")\nTRAIN = PATH/'train'\nTEST = PATH/'test'","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_names = list({f[:36] for f in os.listdir(TRAIN)})\ntest_names = list({f[:36] for f in os.listdir(TEST)})\n\nprint(len(train_names), len(test_names))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e3822dacd626ce595bf8931bf86fc9b73cf7eece"},"cell_type":"code","source":"CHANNELS = np.array(['green', 'red', 'blue', 'yellow'])\nCHANNEL_CMAP = {\"green\": \"Greens\", \"red\": \"Reds\", \"blue\": \"Blues\", \"yellow\": \"Oranges\"}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d28d0be7116e608ff521dc3b97dffa92c0982b2b"},"cell_type":"code","source":"def load_image(img_id, channels, img_dir, suffix='.png', size=512):\n    px = np.zeros(shape=(len(channels),size,size))\n    for i, ch in enumerate(channels):\n        fname = str(img_dir/f'{img_id}_{ch}{suffix}')\n        im = PIL.Image.open(fname)\n        if size < 512:\n            im = im.resize((size, size))\n        px[i,:,:] = np.array(im)\n    px = np.moveaxis(px.astype(np.uint8), 0, 2)\n    return PIL.Image.fromarray(px)\n\ndef show_image(img, channels, title=\"\", subax=None, figsize=(16,5)):\n    px = np.array(img) / 255.\n    px = np.moveaxis(px, 2, 0)\n    if subax==None: fig, subax = plt.subplots(1, len(channels), figsize=figsize)\n    for i, ch in enumerate(channels):\n        subax[i].imshow(px[i], cmap=CHANNEL_CMAP[ch])\n        if i == 0: subax[i].set_title(str(title))\n\ndef save_img(img_id, img, ch, path, suffix=\".png\", save=True):\n    fname = str(path/f'{img_id}_{ch}{suffix}')\n    if save:\n        img.save(fname)\n    return fname\n\ndef make_dir(directory):\n    if not os.path.exists(directory):\n        os.makedirs(directory)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"b2f75c97ce0ae2bcbea1c1badd9cc7c2ce1befc5"},"cell_type":"markdown","source":"## Sanity check"},{"metadata":{"trusted":true,"_uuid":"94e9cd541ff2e896628b932f08b9e2a3265dd269","_kg_hide-output":true,"_kg_hide-input":true},"cell_type":"code","source":"img_id = train_names[0]\nchannels = CHANNELS\nimg = load_image(img_id, CHANNELS, TRAIN, size=299)\nshow_image(img, channels)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"88004b84b6aff35bf4347e3830237f575c765ab4"},"cell_type":"code","source":"make_dir(\"tmp\")\nchannels = CHANNELS\nsave_channel = \"\".join([ch[0] for ch in channels]);\n\nfname = f'tmp/{img_id}_{save_channel}.png'\nprint(\"Saving to\", fname)\n\nimg.save(fname)\nimg_read = PIL.Image.open(fname)\nprint(\"Same images\", np.allclose(img, img_read))\nprint(\"image shape\", np.array(img_read).shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0e736182e82633ae9fd06f7414b6a5d1e397a3da"},"cell_type":"markdown","source":"## Combine test images"},{"metadata":{"_uuid":"16fb88f629a17c3fcb522e58611541c71499706e"},"cell_type":"markdown","source":"### choose channels"},{"metadata":{"trusted":true,"_uuid":"08bff04e7e1f7292ee6e8025ec5a4c9bd74e59bd"},"cell_type":"code","source":"channels = CHANNELS[[0, 1, 3]]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"ed6578b175ca53d3ce56710774eeb7b47a621d24"},"cell_type":"code","source":"save_channel = \"\".join([ch[0] for ch in channels]);\nmake_dir(f\"test_{save_channel}\")\n# Kaggle doesn't seem to create folder, but output files are visible\n# Running for just 10 test imags\nfor img_id in tqdm(test_names[0:5]):\n    img = load_image(img_id, channels, TEST, size=512)\n    fname = f'test_{save_channel}/{img_id}_{save_channel}.png'\n    img.save(fname)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"ca0bc1843a7889319bfbc6987bf50877177e7556"},"cell_type":"markdown","source":"## Combine train images"},{"metadata":{"trusted":true,"_uuid":"c7f12112b8f74497cf3bd5cb83f124c5d35f5aed"},"cell_type":"code","source":"save_channel = \"\".join([ch[0] for ch in channels]);\nmake_dir(f\"train_{save_channel}\")\n# Kaggle doesn't seem to create folder, but output files are visible\n# Running for just 10 train imags\nfor img_id in tqdm(train_names[0:5]):\n    img = load_image(img_id, channels, TRAIN, size=512)\n    fname = f'train_{save_channel}/{img_id}_{save_channel}.png'\n    img.save(fname)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"975e9df25cfa1384ae92f6f844b84ff0b0fb8fc0"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}