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 ...

Get Learning Geospatial Analysis with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.