{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Get EXIF data from image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image, ExifTags"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def print_exif(img_path):\n",
    "    img = Image.open(img_path)\n",
    "    img_exif = img.getexif()\n",
    "\n",
    "    for key, val in img_exif.items():\n",
    "        if key in ExifTags.TAGS:\n",
    "            print(f\"{ExifTags.TAGS[key]} ({key}): {val}\")\n",
    "        else:\n",
    "            print(f\"(unknown) {key}: {val}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PrintImageMatching (50341): b'PrintIM\\x000300\\x00\\x00\\x00\\x03\\x00\\x01\\x00\"\\x00\"\\x00\\n\\x00\\x00\\x00\\x00\\x00\\x0b\\x00\\x00\\x00y\\x00\\x00\\x00\\x00\\x00\\x00'\n",
      "ResolutionUnit (296): 2\n",
      "ExifOffset (34665): 158\n",
      "ImageDescription (270): DCIM\\101RECNX\n",
      "Make (271): RECONYX\n",
      "Model (272): UltraFire\n",
      "Software (305): 1.6.20190312a\u0000\u0000\u0000\u0000\u0000\n",
      "Orientation (274): 1\n",
      "DateTime (306): 2021:04:26 05:25:21\n",
      "YCbCrPositioning (531): 1\n",
      "XResolution (282): 72.0\n",
      "YResolution (283): 72.0\n"
     ]
    }
   ],
   "source": [
    "print_exif(\"sample.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ResolutionUnit (296): 2\n",
      "ExifOffset (34665): 240\n",
      "Make (271): CUDDEBACK\n",
      "Model (272): G-2+\n",
      "Software (305): 1200 FW 7.10.0\n",
      "DateTime (306): 2021:06:27 23:01:30\n",
      "Orientation (274): 1\n",
      "YCbCrPositioning (531): 2\n",
      "Copyright (33432): Copyright Cuddeback, 2021\n",
      "XResolution (282): 72.0\n",
      "YResolution (283): 72.0\n"
     ]
    }
   ],
   "source": [
    "print_exif(\"sample2.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "def get_image_date(img_path):\n",
    "    img = Image.open(img_path)\n",
    "    date_raw = img.getexif()[306]\n",
    "    return datetime.strptime(date_raw, \"%Y:%m:%d %H:%M:%S\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2021, 4, 26, 5, 25, 21)"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_image_date(\"sample.jpg\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
  },
  "kernelspec": {
   "display_name": "Python 3.6.9 64-bit",
   "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.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
