Who Wants To Write a Little Code?

This post originally appeared on the Software Carpentry website.

We have always steered away from building libraries to use in teaching—we want to show people the "real" stuff, and we can't afford to maintain things. The IPython Notebook has us rethinking that: now that we can display images inline, it would be wonderful if we could leverage Mark Guzdial and Barbara Ericson's work, and teach basic Python using simple image manipulation for examples (rather than text munging). Their research found that using images led to higher retention: more students stuck around, and the ones who did, remembered and used more of what they'd learned.

The problem is, libraries like PIL and scikit-image aren't novice-friendly or teaching-oriented. Again, drawing from Guzdial and Ericson's work (which my co-authors and I did in Practical Programming), we want something like this:

>>> from skimage import novice         # special submodule for beginners
>>> picture = novice.open('kite.png')  # create a picture object from a file
>>> print picture.format  # pictures know their format...
'png'
>>> print picture.path                 # ...and where they came from...
'/Users/example/kite.png'
>>> print picture.size                 # ...and their size
(400, 500)
>>> print picture.width                # 'width' and 'height' also exposed
400
>>> picture.size = (200, 250)          # changing size automatically resizes
>>> for pixel in picture:              # can iterate over pixels
...     if (pixel.red > 0.5) and \     # pixels have RGB (values are 0.0-1.0)...
...         (pixel.x < picture.width):   # ...and know where they are
...         pixel.red /= 2               # pixel is an alias into the picture
...
>>> print picture.modified               # pictures know if their pixels are dirty
True
>>> print picture.path                   # picture no longer corresponds to file
None
>>> picture[0:20, 0:20] = (0., 0., 0.)   # overwrite lower-left rectangle with black
>>> picture.save('kite-bluegreen.jpg')   # guess file type from suffix
>>> print picture.path                   # picture now corresponds to file
'/Users/example/kite-bluegreen.jpg'
>>> print picture.modified               # and is now in sync
False

The key thing here is that the iterator variable aliases the pixel, but has extra information (its XY coordinates). Yes, this is a bit odd if you're used to standard image processing libraries, or worried about performance, but this is what works best for teaching.

If this existed, we'd rewrite our intro Python material (variables, loops, conditionals, functions) on top of it. My question is, would any of our regular readers like to build it for us?

Dialogue & Discussion

Comments must follow our Code of Conduct.

Edit this page on Github