{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "0ed59a68-3273-6607-5481-f2d3824ba619"
      },
      "source": [
        "Image data processing with numpy"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "1aed3601-17a9-5d43-d363-db14f125e6c1"
      },
      "outputs": [],
      "source": [
        "def read_image(filepath):\n",
        "    im = io.imread(filepath)\n",
        "    # scale\n",
        "    (w, h,_) = im.shape\n",
        "    if (w>h): # height is greater than width\n",
        "        resizeto = (IMAGE_SIZE, int (round (IMAGE_SIZE * (float (h)  / w))));\n",
        "    else:\n",
        "        resizeto = (int (round (IMAGE_SIZE * (float (w)  / h))), IMAGE_SIZE);\n",
        "    im = misc.imresize(im, resizeto, interp='bicubic').astype(np.float32) # it's float32 as of now\n",
        "    # swap RBG->BGR\n",
        "    im = im[:, :, ::-1]\n",
        "    \n",
        "    # remove mean (sueezenet mean values)\n",
        "    im[:, :, 0] -= 104.006\n",
        "    im[:, :, 1] -= 116.669\n",
        "    im[:, :, 2] -= 122.679\n",
        "\n",
        "    \n",
        "    # padd with 0 (since we're already mean centered the 0 is correct value for padding)\n",
        "    paddim=np.array((IMAGE_SIZE,IMAGE_SIZE))-np.array(resizeto)\n",
        "    npad=((0,paddim[0]),(0,paddim[1]),(0,0))\n",
        "    im = np.pad(im,npad, mode='constant')\n",
        "    return im\n",
        "\n",
        "def unprocess_image(im):   \n",
        "    # restore mean (sueezenet mean values)\n",
        "    im[:, :, 0] += 104.006\n",
        "    im[:, :, 1] += 116.669\n",
        "    im[:, :, 2] += 122.679\n",
        "    \n",
        "    # swap RBGR->RBG\n",
        "    im = im[:, :, ::-1]\n",
        "    \n",
        "    return im"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "adee638f-4ed7-68aa-618d-d734ba50d142"
      },
      "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
}