{"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_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)\nimport matplotlib.pyplot as plt\nimport cv2\nfrom scipy.ndimage import label\nfrom skimage.io import imsave, imread\nimport tqdm\nimport warnings\nimport os\n\nimport zipfile","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../input","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"warnings.filterwarnings('ignore')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"z = zipfile.ZipFile('../input/0417-external-rare-cell-mask-generation/external_mask.zip')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def squarify(M,val):\n    (a,b,c)=M.shape\n    if a>b:\n        padding=((0,0),((a-b)//2,a-b-(a-b)//2),(0, 0))\n    else:\n        padding=(((b-a)//2,b-a-(b-a)//2),(0,0),(0, 0))\n    return np.pad(M,padding,mode='constant',constant_values=val)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"compress = zipfile.ZipFile('./result.zip', 'w')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"SZ = 2048\n\n\nfor x in tqdm.tqdm_notebook(z.namelist()[:10000]):\n    if 'npz' in x:\n        iid = x.split('/')[1].split('.')[0]\n        mask = np.load(z.open(x))['arr_0']\n\n        r = cv2.imread(f'../input/hpa-0417-rare-jpeg-collect/publichpa/{iid}_red.jpg', 0)\n        g = cv2.imread(f'../input/hpa-0417-rare-jpeg-collect/publichpa/{iid}_green.jpg', 0)\n        b = cv2.imread(f'../input/hpa-0417-rare-jpeg-collect/publichpa/{iid}_blue.jpg', 0)\n        a = cv2.imread(f'../input/hpa-0417-rare-jpeg-collect/publichpa/{iid}_yellow.jpg', 0)\n        if not r.shape[0] == SZ:\n            r = cv2.resize(r, (SZ, SZ))\n            g = cv2.resize(g, (SZ, SZ))\n            b = cv2.resize(b, (SZ, SZ))\n            a = cv2.resize(a, (SZ, SZ))\n        img = np.stack([r, g, b, a], -1)\n\n        for i in range(1, mask.max()):\n            sub_mask = cv2.resize((mask == i).astype(np.uint8), (SZ, SZ))\n            x, y = np.where(sub_mask == 1)\n            sub = img[x.min(): x.max(), y.min(): y.max()]\n            crop_sub_mask = sub_mask[x.min(): x.max(), y.min(): y.max()]\n            crop_sub_mask = np.repeat(crop_sub_mask[:, :, np.newaxis], 4, axis=2)\n            r = sub * crop_sub_mask\n            r = squarify(r, 0)\n            if r.shape[0] > 256:\n                r = cv2.resize(r, (256, 256))\n            imsave(f'./{iid}_{i}.png', r)\n            compress.write(f'./{iid}_{i}.png')\n            os.remove(f'./{iid}_{i}.png')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}