{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install imagecodecs\n!pip install glob3","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"from pathlib import Path\nimport multiprocessing\nimport scipy\nimport os\nfrom PIL import Image\nimport numpy as np\nimport skimage.io\nimport tqdm\nimport glob\nimport cv2\n\nImage.MAX_IMAGE_PIXELS = None","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"def crop_white(image, mask, value=255):\n    ys, = (image.min((1, 2)) < value).nonzero()\n    xs, = (image.min(0).min(1) < value).nonzero()\n    if len(xs) == 0 or len(ys) == 0:\n        return image, mask\n    new_image = image[ys.min():ys.max() + 1, xs.min():xs.max() + 1]\n    new_mask = mask[ys.min():ys.max() + 1, xs.min():xs.max() + 1]\n    return new_image, new_mask\n\n\ndef to_jpeg(mask_path):\n    path = os.path.join(\n        '../input/prostate-cancer-grade-assessment/train_images',\n        os.path.basename(mask_path)[:-10] + '.tiff'\n    )\n    image = skimage.io.MultiImage(path)\n    image_mask = skimage.io.MultiImage(mask_path)\n    image_to_jpeg(mask_path[:-5] + '_1', image[1], image_mask[1][:,:,0])\n    image_to_jpeg(mask_path[:-5] + '_2', image[2], image_mask[2][:,:,0])\n\n\ndef image_to_jpeg(mask_path, image, image_mask):\n    image, image_mask = crop_white(image, image_mask)\n    image_mask = scipy.sparse.csc_matrix(image_mask)\n    scipy.sparse.save_npz(os.path.join('image_masks/', os.path.basename(mask_path)), image_mask)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.makedirs('image_masks', exist_ok=True)\npaths = glob.glob('../input/prostate-cancer-grade-assessment/train_label_masks/*.tiff')\nwith multiprocessing.Pool(processes=4) as pool:\n    for _ in tqdm.tqdm(pool.imap(to_jpeg, paths), total=len(paths)):\n        pass","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tarfile\ndef make_tarfile(output_filename, source_dir):\n    with tarfile.open(output_filename, \"w:gz\") as tar:\n        tar.add(source_dir, arcname=os.path.basename(source_dir))\n        \nmake_tarfile('image_masks.tar.gz','image_masks')\nimport shutil\nshutil.rmtree('image_masks')","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}