{ "metadata": { "name": "starry_night_to_text" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "JPEG to Text and Blocks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Converting a `.jpg` image to a text file containing data useable with [`ipythonblocks`](https://github.com/jiffyclub/ipythonblocks)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from PIL import Image" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open up an [image](http://www.moma.org/collection_images/resized/075/w500h420/CRI_133075.jpg) of Starry Night by Vincent van Gogh that I pulled from the [MOMA website](http://www.moma.org/collection/object.php?object_id=79802)." ] }, { "cell_type": "code", "collapsed": false, "input": [ "im = Image.open('starry_night.jpg')" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "im.size" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 3, "text": [ "(500, 398)" ] } ], "prompt_number": 3 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The image is a bit bigger than I want for use with `ipythonblocks`. Tables on the order of 100x100 seem to be big enough without slowing down the browser too much. Cutting each dimension in half to 125x100 should do the trick. Always use antialiasing when decreasing the size of an image." ] }, { "cell_type": "code", "collapsed": false, "input": [ "im = im.resize((125, 100), Image.ANTIALIAS)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 4 }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `getdata()` method gives me an iterable sequence of RGB tuples starting with the top left image pixel and going down the image row by row." ] }, { "cell_type": "code", "collapsed": false, "input": [ "imdata = im.getdata()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "imdata[0]" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 6, "text": [ "(121, 109, 84)" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "len(imdata)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "pyout", "prompt_number": 7, "text": [ "12500" ] } ], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "I want to write out a file with the following format:\n", "\n", " # width height\n", " 125 100\n", " # block size\n", " 4\n", " # initial color\n", " 0 0 0\n", " # row column red green blue\n", " 0 0 121 109 84\n", " ..." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import os\n", "import itertools" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "code", "collapsed": false, "input": [ "with open('starry_night.txt', 'w') as f:\n", " s = ['# width height', '{0} {1}'.format(im.size[0], im.size[1]),\n", " '# block size', '4',\n", " '# initial color', '0 0 0',\n", " '# row column red green blue']\n", " f.write(os.linesep.join(s) + os.linesep)\n", " \n", " for ((row, col), colors) in itertools.izip(itertools.product(xrange(im.size[1]), xrange(im.size[0])), imdata):\n", " things = [str(x) for x in (row, col) + colors]\n", " f.write(' '.join(things + ['\\n']))" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make sure it worked..." ] }, { "cell_type": "code", "collapsed": false, "input": [ "!head starry_night.txt" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "# width height\r\n", "125 100\r\n", "# block size\r\n", "4\r\n", "# initial color\r\n", "0 0 0\r\n", "# row column red green blue\r\n", "0 0 121 109 84 \r\n", "0 1 122 115 99 \r\n", "0 2 117 110 96 \r\n" ] } ], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "And, for kicks, display `imdata` with `ipythonblocks`." ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ipythonblocks import BlockGrid" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 11 }, { "cell_type": "code", "collapsed": false, "input": [ "grid = BlockGrid(125, 100, block_size=4, lines_on=False)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "code", "collapsed": false, "input": [ "for block, colors in itertools.izip(grid, imdata):\n", " block.rgb = colors" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "code", "collapsed": false, "input": [ "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "pyout", "prompt_number": 14, "text": [ "" ] } ], "prompt_number": 14 } ], "metadata": {} } ] }