On 07/22/2010 03:40 PM, j vickroy wrote:
> Christopher Barker wrote:
>> Jim Vickroy wrote:
>>
>>> The attachment is a simple script that creates a 2D array of unsigned,
>>> 8-bit integers and uses matplotlib to save it as a PNG file.
>>>
>>> Unfortunately, the PNG file is much larger than expected -- apparently
>>> because it is True-Color; on my MS Windows machine, bit depth, for the
>>> file, is listed as 32 rather than the expected 8.
>>>
>>
>>
>>> Can matplotlib  be used to accomplish this?  If so, could someone direct
>>> me to where this is discussed?
>>>
>>
>> I don't think so directly. MPL uses a 32 bit image buffer internally,
>> and that's what gets saved out in the PNG.
>>
>> You can post-process the image with something like ImageMagick.
>>
>> Another alternative is to use PIL -- you can grab the matplotlib buffer,
>> make a PIL image out of it, and use PIL to convert to an 8-bit palleted
>> image.
>>
>> For that matter, you could probably bypass MPL, and use numpy to create
>> the 8-bit image you want, and PIL to save it as a PNG.
>>
>> -Chris
>>
>> Thanks much for the helpful information.  I will revisit PIL; I tried 
>> matplotlib because of other requirements (colorbar, various figure 
>> annotations) which did not appear to be readily available in PIL.  At this 
>> stage, it depends on how important the requirement is to reduce the size of 
>> the PNG images.
>>
> -- jv

No need to use anything other than matplotlib for generating the 
figure--Chris's main point is that mpl does not provide configuration 
options for saving png files, but you can take such a png file, complete 
with image, colorbar, annotations, whatever, and use pil or ImageMagick 
(and there are probably several additional alternatives) to convert to a 
more compact png format.  I took a quick look at the documentation of 
pil and IM, but unfortunately did not see any nice example of this--it 
is not obvious to me how to generate the palette.  I suspect it is not 
hard, but it might take a while to figure it out from the docs.

Programs like the following might do what you want:
http://www.libpng.org/pub/png/apps/pngquant.html
http://pngnq.sourceforge.net/

Eric

>>
>>
>>
>
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Sprint
> What will you do first with EVO, the first 4G phone?
> Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
>
>
>
> _______________________________________________
> Matplotlib-users mailing list
> Matplotlib-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/matplotlib-users


------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to