[mapserver-users] [Python+MapScript] Convert imageObj to PIL Image

Jelmer Baas baas at speerit.nl
Fri Sep 7 06:35:56 PDT 2012


Hi All,

I've been testing today in Python, trying to get a MapScript imageObj into an python Image object. This works via the following code:


        map.transparent = MS_ON
        map.selectOutputFormat('png8')
        map.outputformat.imagemode = MS_IMAGEMODE_RGBA
        map.outputformat.transparent = MS_ON

        img = map.draw()
        mem_png = StringIO.StringIO(img.saveToString())
        _img = Image.open(mem_png)

However, the _img file lost its transparency, even though it's still (or again) PNG. There MUST be a better way of getting the PNG file into PIL, right? Something besides writing it to a tempfile, opening it, and deleting it?

On a side note, I only use this rendered image temporarily, I only need to cut it up in smaller bits and return those to my client. So if there's some sort of format that supports transparcency but without the PNG compression overhead, that would be even better! 

Regards,
Jelmer Baas




More information about the MapServer-users mailing list