{"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":"markdown","source":"# Image processing with `pyVips` 📦 (even offline)\n\nUsing this dataset: https://www.kaggle.com/datasets/jirkaborovec/pyvips-python-and-deb-package\n\nIf you need to download latest version check: https://www.kaggle.com/code/jirkaborovec/pip-pkg-pyvips-download-4-offline\n\n## How to install 📦 | step-by-step:\n\n1. Click \"Add Data\" on your own notebook\n2. Search for dataset pyVips: python & deb package\n3. Run those installation lines below:","metadata":{}},{"cell_type":"code","source":"!ls /kaggle/input/pyvips-python-and-deb-package\n# intall the deb packages\n!dpkg -i --force-depends /kaggle/input/pyvips-python-and-deb-package/linux_packages/archives/*.deb\n# install the python wrapper\n!pip install pyvips -f /kaggle/input/pyvips-python-and-deb-package/python_packages/ --no-index","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-10-29T09:05:56.00977Z","iopub.execute_input":"2023-10-29T09:05:56.010219Z","iopub.status.idle":"2023-10-29T09:07:15.853637Z","shell.execute_reply.started":"2023-10-29T09:05:56.01018Z","shell.execute_reply":"2023-10-29T09:07:15.852249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Installing on GPU [alternative]\n\nNote, the GPU machine requires a bit different set of packages so in that case use the code bellow... ","metadata":{}},{"cell_type":"code","source":"# !ls /kaggle/input/pyvips-python-and-deb-package-gpu\n# # intall the deb packages\n# !yes | dpkg -i --force-depends /kaggle/input/pyvips-python-and-deb-package-gpu/linux_packages/archives/*.deb\n# # install the python wrapper\n# !pip install pyvips -f /kaggle/input/pyvips-python-and-deb-package-gpu/python_packages/ --no-index","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:07:15.856985Z","iopub.execute_input":"2023-10-29T09:07:15.858189Z","iopub.status.idle":"2023-10-29T09:07:15.864514Z","shell.execute_reply.started":"2023-10-29T09:07:15.858145Z","shell.execute_reply":"2023-10-29T09:07:15.863298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os, glob\n\nos.environ['VIPS_CONCURRENCY'] = '4'\n# set VIPS_DISC_THRESHOLD to a small number to prefer the use of disc,\n#  set it to a large number to prefer RAM\nos.environ['VIPS_DISC_THRESHOLD'] = '15gb'\n\n!pip list | grep pyvips","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:07:15.866216Z","iopub.execute_input":"2023-10-29T09:07:15.866539Z","iopub.status.idle":"2023-10-29T09:07:40.953782Z","shell.execute_reply.started":"2023-10-29T09:07:15.866512Z","shell.execute_reply":"2023-10-29T09:07:40.952699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pyvips\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:07:40.957786Z","iopub.execute_input":"2023-10-29T09:07:40.958271Z","iopub.status.idle":"2023-10-29T09:07:41.388024Z","shell.execute_reply.started":"2023-10-29T09:07:40.958221Z","shell.execute_reply":"2023-10-29T09:07:41.386596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# UBC Ovarian Cancer Subtype 🔬 Classification","metadata":{}},{"cell_type":"code","source":"PATH_IMAGES = \"/kaggle/input/UBC-OCEAN/train_images\"\nls = glob.glob(os.path.join(PATH_IMAGES, \"*.png\"))\nprint(f\"found images: {len(ls)}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:07:41.389588Z","iopub.execute_input":"2023-10-29T09:07:41.389983Z","iopub.status.idle":"2023-10-29T09:07:41.538917Z","shell.execute_reply.started":"2023-10-29T09:07:41.389951Z","shell.execute_reply":"2023-10-29T09:07:41.537986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_img_path = ls[-1]\nprint(f\"sample image: {sample_img_path}\")\nimg = pyvips.Image.new_from_file(sample_img_path)\nprint(f\"image dimension: width={img.width} ; height={img.height}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:07:41.54037Z","iopub.execute_input":"2023-10-29T09:07:41.540996Z","iopub.status.idle":"2023-10-29T09:07:41.578033Z","shell.execute_reply.started":"2023-10-29T09:07:41.540965Z","shell.execute_reply":"2023-10-29T09:07:41.576852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## show 🖼️ thumbnail","metadata":{}},{"cell_type":"code","source":"thumbnail = pyvips.Image.thumbnail(sample_img_path, 3000).numpy()\n_= plt.imshow(thumbnail)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:07:41.579574Z","iopub.execute_input":"2023-10-29T09:07:41.579914Z","iopub.status.idle":"2023-10-29T09:08:18.832902Z","shell.execute_reply.started":"2023-10-29T09:07:41.579885Z","shell.execute_reply":"2023-10-29T09:08:18.831674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## show 🖽 center crop\n\nbrowse the Image attributes: https://libvips.github.io/pyvips/vimage.html#attributes","metadata":{}},{"cell_type":"code","source":"x = int(img.width / 2)\ny = int(img.height / 2)\nw, h = 1600, 900\nprint(f\"crop for: {x=} + {w=}; {y=} + {h=}\")\n\n# just in case add bounds for w and h not exceed the image size\ntile = img.crop(x, y, min(w, img.width - x), min(h, img.height - y))\n_= plt.imshow(tile)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:08:18.834536Z","iopub.execute_input":"2023-10-29T09:08:18.835766Z","iopub.status.idle":"2023-10-29T09:08:45.920687Z","shell.execute_reply.started":"2023-10-29T09:08:18.83572Z","shell.execute_reply":"2023-10-29T09:08:45.919207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Mayo Clinic - STRIP AI","metadata":{}},{"cell_type":"code","source":"PATH_IMAGES = \"/kaggle/input/mayo-clinic-strip-ai/train\"\nls = glob.glob(os.path.join(PATH_IMAGES, \"*.tif\"))\nprint(f\"found images: {len(ls)}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:08:45.922376Z","iopub.execute_input":"2023-10-29T09:08:45.922854Z","iopub.status.idle":"2023-10-29T09:08:46.141178Z","shell.execute_reply.started":"2023-10-29T09:08:45.922819Z","shell.execute_reply":"2023-10-29T09:08:46.140245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_img_path = ls[-1]\nprint(f\"sample image: {sample_img_path}\")\nimg = pyvips.Image.new_from_file(sample_img_path)\nprint(f\"image dimension: width={img.width} ; height={img.height}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:08:46.144536Z","iopub.execute_input":"2023-10-29T09:08:46.145497Z","iopub.status.idle":"2023-10-29T09:08:46.178381Z","shell.execute_reply.started":"2023-10-29T09:08:46.145458Z","shell.execute_reply":"2023-10-29T09:08:46.177001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## show 🖼️ thumbnail","metadata":{}},{"cell_type":"code","source":"thumbnail = pyvips.Image.thumbnail(sample_img_path, 3000).numpy()\n_= plt.imshow(thumbnail)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:08:46.179943Z","iopub.execute_input":"2023-10-29T09:08:46.180467Z","iopub.status.idle":"2023-10-29T09:09:32.66916Z","shell.execute_reply.started":"2023-10-29T09:08:46.180437Z","shell.execute_reply":"2023-10-29T09:09:32.667689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## show 🖽 center crop\n\nbrowse the Image attributes: https://libvips.github.io/pyvips/vimage.html#attributes","metadata":{}},{"cell_type":"code","source":"x = int(img.width / 3)\ny = int(img.height / 3)\nw, h = 1600, 900\nprint(f\"crop for: {x=} + {w=}; {y=} + {h=}\")\n\n# just in case add bounds for w and h not exceed the image size\ntile = img.crop(x, y, min(w, img.width - x), min(h, img.height - y))\n_= plt.imshow(tile)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:09:32.670969Z","iopub.execute_input":"2023-10-29T09:09:32.671326Z","iopub.status.idle":"2023-10-29T09:09:33.5029Z","shell.execute_reply.started":"2023-10-29T09:09:32.671289Z","shell.execute_reply":"2023-10-29T09:09:33.501652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# HuBMAP + HPA - Hacking the Human Body","metadata":{}},{"cell_type":"code","source":"PATH_IMAGES = \"/kaggle/input/hubmap-organ-segmentation/train_images\"\nls = glob.glob(os.path.join(PATH_IMAGES, \"*.tiff\"))\nprint(f\"found images: {len(ls)}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:09:33.504105Z","iopub.execute_input":"2023-10-29T09:09:33.504469Z","iopub.status.idle":"2023-10-29T09:09:33.560285Z","shell.execute_reply.started":"2023-10-29T09:09:33.504437Z","shell.execute_reply":"2023-10-29T09:09:33.559296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_img_path = ls[-1]\nprint(f\"sample image: {sample_img_path}\")\nimg = pyvips.Image.new_from_file(sample_img_path)\nprint(f\"image dimension: width={img.width} ; height={img.height}\")","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:09:33.561627Z","iopub.execute_input":"2023-10-29T09:09:33.561962Z","iopub.status.idle":"2023-10-29T09:09:33.576426Z","shell.execute_reply.started":"2023-10-29T09:09:33.561927Z","shell.execute_reply":"2023-10-29T09:09:33.575371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## show 🖼️ thumbnail","metadata":{}},{"cell_type":"code","source":"thumbnail = pyvips.Image.thumbnail(sample_img_path, 2000).numpy()\n_= plt.imshow(thumbnail)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:09:33.577941Z","iopub.execute_input":"2023-10-29T09:09:33.578249Z","iopub.status.idle":"2023-10-29T09:09:35.099435Z","shell.execute_reply.started":"2023-10-29T09:09:33.57822Z","shell.execute_reply":"2023-10-29T09:09:35.098256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## show 🖽 center crop\n\nbrowse the Image attributes: https://libvips.github.io/pyvips/vimage.html#attributes","metadata":{}},{"cell_type":"code","source":"x = int(img.width / 2)\ny = int(img.height / 2)\nw, h = 1600, 900\nprint(f\"crop for: {x=} + {w=}; {y=} + {h=}\")\n\n# just in case add bounds for w and h not exceed the image size\ntile = img.crop(x, y, min(w, img.width - x), min(h, img.height - y))\n_= plt.imshow(tile)","metadata":{"execution":{"iopub.status.busy":"2023-10-29T09:09:35.100773Z","iopub.execute_input":"2023-10-29T09:09:35.101129Z","iopub.status.idle":"2023-10-29T09:09:35.904852Z","shell.execute_reply.started":"2023-10-29T09:09:35.101097Z","shell.execute_reply":"2023-10-29T09:09:35.903895Z"},"trusted":true},"execution_count":null,"outputs":[]}]}