PIL
The PIL was originally developed for remote sensing but has evolved as a general image editing library for Python. Like NumPy, it is written in C for speed, but is designed specifically for Python. In addition to image creation and processing, it also has a useful raster drawing module. PIL is also available via PyPI.
In this example, we'll combine PyShp and PIL to rasterize the hancock
shapefile from previous examples and save it as an image. We'll use a "world to pixel" coordinate transformation similar to our SimpleGIS from Chapter 1, Learning Geospatial Analysis with Python. We'll create an image to use as a canvas in PIL and then use the PIL ImageDraw
module to render the polygon. Finally we'll save it as a PNG image:
>>> import shapefile >>> import Image, ImageDraw >>> r = shapefile.Reader("hancock.shp") >>> xdist = r.bbox[2] - r.bbox[0] >>> ydist = r.bbox[3] - r.bbox[1] >>> iwidth = 400 >>> iheight = 600 >>>...