Python Appendix

Release:4.2
Revision: 1.4.2.1
Date: 2004-05-25
Author: Sean Gillies
Contact: sgillies@frii.com

Introduction

The Python mapscript module contains some class extension methods that have not yet been implemented for other languages.

Classes

References to sections below will be added here as the documentation grows.

imageObj

imageObj

The Python Imaging Library, http://www.pythonware.com/products/pil/, is an indispensible tool for image manipulation. The extensions to imageObj are all geared towards better integration of PIL in mapscript applications.

Methods

imageObj(int width, int height [, string driver [, PyObject file]]) : imageObj

Create a new instance which is either empty or read from a Python file-like object. If file is provided, the size of the file's image override any other parameters passed to the method. The driver must be specified as well as file as the method does not auto-detect the file's image format.

If file is not specified an imageObj is created with the specified dimensions and of the format specified by driver or of a default format obtained by trying "GD/GIF", "GD/PNG", and "GD/JPEG" in that order.

Interesting values of file to try are instances of StringIO

s = StringIO()
pil_image.save(s)    # Save an image manipulated with PIL
ms_image = imageObj(0, 0, 'GD/PNG', s)

Or the file-like object returned from urlopen

url = urllib.urlopen('http://mapserver.gis.umn.edu/bugs/ant.jpg')
ms_image = imageObj(0, 0, 'GD/JPEG', url)
saveToString() : string

Write the image data to a string rather than to a file. What's the use of this? Consider the following pointless example :)

data = imgobj.saveToString()
fh = open('foo.%s' % imgobj.format.extension, 'wb')
fh.write(data)
fh.close()

Or the more useful example using PIL

data = imgobj.saveToString()

from StringIO import StringIO
from PIL import Image

pil_image = Image.open(StringIO(data))

# Now do whatever you want using PIL

This method will be deprecated as soon as we change the save() method of imageObj to save to Python file-like objects, allowing

s = StringIO()