{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy as np\n",
    "from PIL import Image, ImageFile\n",
    "from skimage import io\n",
    "import matplotlib.pyplot as plt\n",
    "import psycopg2\n",
    "import tifffile\n",
    "from scipy import misc\n",
    "from tifffile import TiffFile\n",
    "from tifffile.tifffile import TIFF_DECOMPESSORS\n",
    "from configparser import ConfigParser\n",
    "from batch_tiff_to_jpeg import tiff_to_jpg\n",
    "from psycopg2.extras import DictCursor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "conn = None\n",
    "def get_connection():\n",
    "    \"\"\"\n",
    "    :rtype: connection\n",
    "    \"\"\"\n",
    "    global conn\n",
    "    if conn is None:\n",
    "        CONFIG = ConfigParser()\n",
    "        CONFIG.read(\"gta-postprocessing.local.ini\")\n",
    "        conn = psycopg2.connect(CONFIG[\"Postgres\"][\"db\"], cursor_factory=DictCursor)\n",
    "    return conn\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generating jpeg for info-2017-12-08--14-51-34--757.tiff\n"
     ]
    }
   ],
   "source": [
    "CONFIG = ConfigParser()\n",
    "CONFIG.read(\"gta-postprocessing.local.ini\")\n",
    "tiff_dir = CONFIG[\"Images\"][\"Tiff\"]\n",
    "out_dir = './../other_things/weather_2'\n",
    "if not os.path.exists(out_dir):\n",
    "    os.makedirs(out_dir)\n",
    "\n",
    "conn = get_connection()\n",
    "cur = conn.cursor()\n",
    "min_run = 39\n",
    "cur.execute(\"\"\"SELECT DISTINCT ON(currentweather) \\\n",
    "    currentweather, imagepath \\\n",
    "    FROM snapshots \\\n",
    "    WHERE run_id >= '{}'\"\"\".format(min_run))\n",
    "for row in cur:\n",
    "    image_path = row['imagepath']\n",
    "    weather = row['currentweather']\n",
    "    tiff_to_jpg(tiff_dir, out_dir, '{}.jpg'.format(weather), 'info-{}.tiff'.format(image_path), 0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D:\\\\projekty\\\\GTA-V-extractors\\\\output'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tiff_dir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
