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

Jelmer Baas baas at speerit.nl
Fri Sep 7 07:15:22 PDT 2012


Oh, wow, that was so simple. I changed
map.selectOutputFormat('png8')
to
map.selectOutputFormat('png24')

And done. Strangely, though, saving it to a file and re-opening *does* work!

Thanks for the quick reply!

--
Jelmer 

-----Original Message-----
From: thomas bonfort [mailto:thomas.bonfort at gmail.com] 
Sent: Friday, September 07, 2012 15:50
To: Jelmer Baas
Cc: mapserver-users at lists.osgeo.org
Subject: Re: [mapserver-users] [Python+MapScript] Convert imageObj to PIL Image

IIRC, PIL does not support 8bit pngs with transparency, so writing it
to a file won't help. If you need alpha, you'll have to stick with
32bit pngs.

--
thomas

On Fri, Sep 7, 2012 at 3:35 PM, Jelmer Baas <baas at speerit.nl> wrote:
> 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
>
> _______________________________________________
> mapserver-users mailing list
> mapserver-users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/mapserver-users


More information about the mapserver-users mailing list