{ "metadata": { "name": "ipythonblocks_imagegrid" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "ipythonblocks.ImageGrid" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To learn more about `ipythonblocks` visit the homepage at [https://github.com/jiffyclub/ipythonblocks](https://github.com/jiffyclub/ipythonblocks).\n", "\n", "`ImageGrid` is a class that imitates image manipulation libraries like [PIL](http://www.pythonware.com/products/pil/).\n", "Key differences from `BlockGrid` are:\n", "\n", "* Only 2D indexing is supported\n", "* Indices are [column, row]\n", "* Grid units are `Pixel` objects, which have `.x` (column) and `.y` (row) attributes\n", "* Grid origin defaults to the lower-left corner, but can be set to the upper-left" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from ipythonblocks import ImageGrid" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "grid = ImageGrid(10, 10, fill=(124, 124, 124), block_size=15)\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "pyout", "prompt_number": 2, "text": [ "" ] } ], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "for pixel in grid:\n", " if pixel.x < 5:\n", " pixel.green = 255\n", " elif pixel.x > 5 and pixel.y >= 5:\n", " pixel.blue = 255\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "pyout", "prompt_number": 3, "text": [ "" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "grid[:3, :3] = (255, 124, 124)\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "pyout", "prompt_number": 4, "text": [ "" ] } ], "prompt_number": 4 }, { "cell_type": "code", "collapsed": false, "input": [ "grid[::4, ::4] = (0, 0, 0)\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "pyout", "prompt_number": 5, "text": [ "" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [ "grid[6, :] = (255, 0, 255)\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "pyout", "prompt_number": 6, "text": [ "" ] } ], "prompt_number": 6 }, { "cell_type": "code", "collapsed": false, "input": [ "grid[:, 6] = (255, 255, 0)\n", "grid" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
" ], "output_type": "pyout", "prompt_number": 7, "text": [ "" ] } ], "prompt_number": 7 } ], "metadata": {} } ] }