{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Data Extraction\n", "\n", "In the last notebook we made extensive preparations, in order to make sure that an image was ready for reading by code we obtained from a 2012 blog post. The code from Michael Hamilton's post has been modified in some important ways:\n", "\n", "* Updated it for Python 3\n", "* Changed image color mode to 8-bit grayscale\n", "\n", "The modified code has been contributed as a module to the PyPi repository, which means that it is available to python library tools, such as \"pip\" and the module \"punchcards\". In this notebook we are going to run a local copy of punchcards.py, which makes this set of notebooks more portable. We'll run the code on an image that was prepared according to the steps in the [Image Preparation](notebook-2.ipynb) notebook." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Punchcard Text: MACON FORT 4628 NORTH CAROLINA \n", "\n", " Card Dump of Image file: my card Format Dump threshold= 127\n", " 123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-\n", " ________________________________________________________________________________ \n", "/MACON FORT 4628 NORTH CAROLINA |\n", "|.OO...O........................................O.OO...O.O.......................|\n", "|O..OO..OO..................................OOO.....OOO.O........................|\n", "|.........O....................................O.................................|\n", "|.O................................................O.....O.......................|\n", "|...................................O............................................|\n", "|..O......O....................................O..O...O..........................|\n", "|O................................O..............................................|\n", "|....O......................................O...........O........................|\n", "|...O..OO..........................O.........O.......O...........................|\n", "|................................................................................|\n", "|....................................O..........O................................|\n", "|........O....................................O.....O..O.........................|\n", "`--------------------------------------------------------------------------------'\n", " 123456789-123456789-123456789-123456789-123456789-123456789-123456789-123456789-\n", "\n" ] } ], "source": [ "from punchcard import PunchCard\n", "from PIL import Image\n", "image = Image.open('prepared_image.png')\n", "# using 127 or neutral gray as threshold for hole vs. card\n", "card = PunchCard(image, bright=127)\n", "print('Punchcard Text: {}'.format(card.text))\n", "print()\n", "card.dump('my card')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# What Happened?\n", "\n", "A detailed walkthrough of the PunchCard class is an excellent exercise, but that will require a separate series of notebooks. In broad strokes the PunchCard Python class take the following steps:\n", "\n", "1. Finds the edges of the card.\n", "1. Calculate the positions of holes.\n", "1. Measure brightness of each hole position.\n", "1. Interpret the holes as characters by looking up their position in a template.\n", "\n", "You can inspect the detailed code by opening the \"punchcard.py\" file." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# My Notes\n", "(for student notes)\n", "\n", "Return: [Main Notebook](index.ipynb)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.5" } }, "nbformat": 4, "nbformat_minor": 2 }