{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Programming with Python (lab classes)\n",
"#### Vedran Ĺ ego, [vsego.org](http://vsego.org/)\n",
"\n",
"## Content:\n",
"\n",
"1. PIL"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 1.** Write a module with a function `spiral(im, box, r, dx, dy, phi=0, color)` that draws a spiral inside the `PIL.Image` object `im`, defined by the box `box` (4-member tuple), with `r` revolutions, starting at the angle `phi`. The parameters `dx` and `dy` represent how much do the `x` and the `y` radius change in each revolution.\n",
"\n",
"For example, the code\n",
"```python\n",
"im = Image.new(\"RGB\", (400, 300), \"white\")\n",
"spiral(im, (0,150,200,300), 7, 13, 7, color=\"red\")\n",
"spiral(im, (400,0,200,150), 17, 2, 4, color=\"navy\")\n",
"```\n",
"should produce the following image:\n",
"![](12c-pil/spirals.png)\n",
"\n",
"**Hint:** A spiral can be drawn as a sequence of short line segments."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 2.** Write a module for drawing chess board configurations. For example, the image of the final configuration of the [Immortal game](http://en.wikipedia.org/wiki/Immortal_Game) should look like this:\n",
"![Final configuration of the Immortal game](12a-chess.png)\n",
"Choose some appropriate input format and data structures to hold all the needed information. The images for the pieces can be found in the internet (for example, the ones in the [Wikipedia's article](http://en.wikipedia.org/wiki/Chess_piece) can be downloaded as big as $2000{\\rm px} \\times 2000{\\rm px}$). The module should handle resizing of the pieces' images to any user-given dimensions.\n",
"\n",
"Include an argument that will change from whose perspective the board is drawn (from the perspective of White, the bottom row is 1 and the top one is 8, while Black views from the opposite direction, seeing the row 8 at the bottom and the row 1 at the top).\n",
"\n",
"A module like that could be used to draw the whole games (as a series of images or as a single big one) or to make a [GUI](http://en.wikipedia.org/wiki/Graphical_user_interface) application for either playing or analyzing chess."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 3.** Make a program that loads the name of an image file and creates an image containing both the loaded image and its histograms for each of its color components (red, green, and blue). The histogram should be saved as `\"original_filename-hist.original_extension\"` and displayed on the screen. Here is a sample of what it should look like for [this image](12c-pil/img_7840.jpg):\n",
"\n",
"![Image with histagram](12c-pil/img_7840-hist.jpg)\n",
"\n",
"**Hint:** A Matplotlib figure can be directly converted to a PIL drawing (search the internet to find out how), allowing you to directly merge the original image and its histograms into a single image. A less elegant solution is to save the histograms figure as an image, and then open it with PIL to add the original image on top of it."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Problem 4.** Make a program that will generate a random labyrinth with a given number of rows and columns, and draw it with and without a solution (two images), like this:\n",
"\n",
"\n",
"\n",
"Alternatively, you can add a background image:\n",
"\n",
"\n",
"\n",
"**How to generate a random labyrinth?**\n",
"\n",
"While this may seem like a very hard problem, it can be done fairly easily with a bit of help from the graph theory and Python's NetworkX module.\n",
"\n",
"To create a random labyrinth with `r` rows and `c` columns, first create a grid graph with $r \\times c$ nodes, assigning each of the edges a random weight, like this:\n",
"![Basic graph as a grid](12c-pil/test-graph.png)\n",
"The nodes represent the blocks in the labyrinth, while the edges represent the possible locations for the walls or the openings.\n",
"\n",
"Then compute its [minimum spanning tree](http://en.wikipedia.org/wiki/Minimum_spanning_tree) $T$ using [one of these NetworkX functions](https://networkx.github.io/documentation/latest/reference/algorithms.mst.html):\n",
"![Spanning tree; the edges define halls](12c-pil/test-tree.png)\n",
"Notice that the spanning tree defines the halls of our labyrinth.\n",
"\n",
"The random weights ensure that we get different minimum spanning trees for different graphs, and the fact that we are using trees ensures that there are no cycles while the graph/labyrinth remains connected (i.e., there is exactly one path between each pair of blocks).\n",
"\n",
"All that we need to do now is draw this as a labyrinth, using PIL's [`ImageDraw.Draw.rectangle` function](http://effbot.org/imagingbook/imagedraw.htm#tag-ImageDraw.Draw.rectangle). You can also use the [`ImageDraw.Draw.line` function](http://effbot.org/imagingbook/imagedraw.htm#tag-ImageDraw.Draw.line), but this will give you less control over how the walls are drawn.\n",
"\n",
"Note that we need to draw the walls of the labyrinth, while our tree contains its halls, i.e., we need to draw the walls corresponding with the grey edges on the previous image. This means that we need to draw a wall between neightbour blocks $(f,t)$ if and only if $(f,t)$ is **not** and edge in $T$. This might seem like a job for a [complement graph](http://en.wikipedia.org/wiki/Complement_graph) (implemented as [`networkx.complement`](http://networkx.lanl.gov/archive/networkx-1.1/reference/generated/networkx.complement.html)), but it is not, because the complement graph would also contain the edges between the non-neighbour nodes. However, a simple `if edge not in tree.edges()` does the trick.\n",
"\n",
"**How to solve the labyrinth?**\n",
"\n",
"Finally, to generate the solution of the labyrinth, choose two blocks for the enterance and the exit, and find the path between them inside the tree $T$. This path is unique, but NetworkX doesn't have a \"find the unique path in a tree\" functon. However, it does have various [shortest path functions](https://networkx.github.io/documentation/latest/reference/algorithms.shortest_paths.html) which will do the job. For our example, such path looks like this:\n",
"![Minimum path, defining the solution](12c-pil/test-path.png)\n",
"\n",
"Again, all that is left is drawing this solution on the previously drawn image of the labyrinth.\n",
"\n",
"**Note:** In order to print these images easily, PIL can save them directly as PDF files."
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 0
}