{ "cells": [ { "cell_type": "markdown", "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", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from ipythonblocks import ImageGrid" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grid = ImageGrid(10, 10, fill=(124, 124, 124), block_size=15)\n", "grid" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "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" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grid[:3, :3] = (255, 124, 124)\n", "grid" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grid[::4, ::4] = (0, 0, 0)\n", "grid" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grid[6, :] = (255, 0, 255)\n", "grid" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "grid[:, 6] = (255, 255, 0)\n", "grid" ] }, { "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.1" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 1 }