{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "176d477d-5f9b-b9e5-199a-c843a23ee512"
      },
      "outputs": [],
      "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 in \n",
        "\n",
        "import numpy as np # linear algebra\n",
        "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n",
        "\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "75caa922-50ce-6770-323c-0fe68fb30441"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "from PIL import Image, ImageChops\n",
        "import cv2\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n",
        "\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "19e98072-a295-2c8b-0d2b-4a97e8a183c0"
      },
      "outputs": [],
      "source": [
        "from glob import glob #glob is used to read file extensions here\n",
        "basepath = '../input/train/'\n",
        "\n",
        "all_cervix_images = []\n",
        "\n",
        "for path in sorted(glob(basepath + \"*\")):\n",
        "    cervix_type = path.split(\"/\")[-1]\n",
        "    cervix_images = sorted(glob(basepath + cervix_type + \"/*\"))\n",
        "    all_cervix_images = all_cervix_images + cervix_images\n",
        "\n",
        "all_cervix_images = pd.DataFrame({'imagepath': all_cervix_images})\n",
        "all_cervix_images['filetype'] = all_cervix_images.apply(lambda row: row.imagepath.split(\".\")[-1], axis=1)\n",
        "all_cervix_images['type'] = all_cervix_images.apply(lambda row: row.imagepath.split(\"/\")[-2], axis=1)\n",
        "all_cervix_images.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "cf2c0c25-7760-5ede-bcc9-39daf6d14dbb"
      },
      "outputs": [],
      "source": [
        "def trim(im):\n",
        "    bg = Image.new(im.mode, im.size, im.getpixel((0,0)))\n",
        "    diff = ImageChops.difference(im, bg)\n",
        "    diff = ImageChops.add(diff, diff, 2.0, -100)\n",
        "    bbox = diff.getbbox()\n",
        "    if bbox:\n",
        "        return im.crop(bbox)\n",
        "   "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "ab7b3221-cf3e-c4d2-372b-0007d1931e1b"
      },
      "outputs": [],
      "source": [
        "\n",
        "fig = plt.figure(figsize=(12,8))\n",
        "\n",
        "i = 1\n",
        "for t in all_cervix_images['type'].unique():\n",
        "    ax = fig.add_subplot(1,3,i)\n",
        "    f = all_cervix_images[all_cervix_images['type'] == t]['imagepath'].values[0]\n",
        "    plt.imshow(plt.imread(f))\n",
        "    plt.title('sample for cervix {}'.format(t))\n",
        "    \n",
        "    im = Image.open(f).convert(\"L\")\n",
        "    im = trim(im)\n",
        "    arr = np.asarray(im)\n",
        "    if (i==2):\n",
        "        arr.tolist()\n",
        "        print(\"works here\")\n",
        "    plt.imshow(arr, cmap='gray')\n",
        "    plt.title('sample for cervix {}'.format(t))\n",
        "    \n",
        "    i+=1\n",
        "     img = cv2.imread('f',0)\n",
        "        px = img[100,100]\n",
        "        print(px)\n",
        "        blue = img[100,100,0]\n",
        "        print(blue)\n",
        "    \n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "86f0ba84-7d1a-695d-7793-e351da43ebc5"
      },
      "outputs": [],
      "source": [
        "image_name = all_cervix_images['imagepath'].values[2]\n",
        "px = f[100,100]\n",
        "print(px)\n",
        "blue = f[100,100,0]\n",
        "print(blue)\n",
        "    "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "08cd4b44-c4bd-8584-70bd-4bf72ce5cfb7"
      },
      "outputs": [],
      "source": ""
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}