Re: [Matplotlib-users] how to save pseudo-colorized images as 8-bit PNG files

2010-07-23 Thread j vickroy

Eric Firing wrote:

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



Try optipng:

http://optipng.sourceforge.net/

Here I am running it on a gray-scale image created by mpl; it was 
generated in such a way that there are only 100 distinct shades of gray.
You can see that it reduces the file size by quite a bit, converting it 
from RGBA to grayscale.  This is lossless.  For this to work, you have 
to make sure you have no more than 256 distinct colors--they don't have 
to be gray.


-rw-rw-r--  1 efiring efiring  8458 2010-07-22 16:26 grayfig.png
efir...@manini:~$ optipng grayfig.png
OptiPNG 0.6.3: Advanced PNG optimizer.
Copyright (C) 2001-2009 Cosmin Truta.

** Processing: grayfig.png
800x600 pixels, 4x8 bits/pixel, RGB+alpha
Reducing image to 8 bits/pixel, grayscale
Input IDAT size = 8352 bytes
Input file size = 8458 bytes

Trying:
   zc = 9  zm = 8  zs = 0  f = 0IDAT size = 3721
   zc = 9  zm = 8  zs = 0  f = 5IDAT size = 3301
   zc = 9  zm = 8  zs = 1  f = 5IDAT size = 3286

Selecting parameters:
   zc = 9  zm = 8  zs = 1  f = 5IDAT size = 3286

Output IDAT size = 3286 bytes (5066 bytes decrease)
Output file size = 3377 bytes (5081 bytes = 60.07% decrease)

efir...@manini:~$ ll grayfig.png
-rw-rw-r-- 1 efiring efiring 3377 2010-07-22 16:26 grayfig.png


Eric

--
  
Thanks much for this information and also for taking the additional time 
to try the optipng tool.  It is very helpful.


Since the above mentioned PNG generation is one step in a near 
real-time products generation system, I was hoping to avoid the addition 
of another component (i.e., PNG compression) in the stream, but it 
appears unavoidable.


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


Re: [Matplotlib-users] how to save pseudo-colorized images as 8-bit PNG files

2010-07-22 Thread j vickroy

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



  


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


[Matplotlib-users] how to erase items in figures

2008-01-28 Thread j vickroy

Hello users,

I wish to repeatedly re-plot labels and contour data on a figure since 
redrawing the figure is temporally expensive.  The attached script (I 
apologize for its length), hopefully, illustrates a simplified version 
of what I'm trying to do -- contour temporally-varying data on a map 
projection.


I do not understand how to erase the plot label each time the figure is 
to be reused.  I also do not know how to erase the contour-fill 
although, based on the generated PNG files,  it does not, for unknown 
reasons , appear to be necessary.  From some postings, it seems that I 
have to employ collections attributes, but I have not been able to find 
documentation or examples that illustrate this.


My system:

   * matplotlib.__version__: '0.91.2'
   * sys.version: '2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC
 v.1310 32 bit (Intel)]'


Thanks,
-- jv
'''
PURPOSE
   Discover a procedure for eliminating the need to redraw each map projection 
from scratch each there is new data to be plotted.
   Redrawing each map is time-consuming!
AUTHOR
   [EMAIL PROTECTED]
'''

import pylab
from   matplotlib.toolkits.basemap import Basemap
import numpy
import os


parameters = dict(projection='merc', llcrnrlon=-180,urcrnrlon=180, 
llcrnrlat=-80,urcrnrlat=85, lon_0=100,lat_ts=20, resolution='c')
map= Basemap(**parameters)
map.drawcoastlines()
map.drawmapboundary()
map.drawmeridians(pylab.arange(0,360,30))
map.drawparallels(pylab.arange(-90,90,30))

parameters = dict(num=1, figsize=(8, 8), dpi=80, facecolor='w', edgecolor='k', 
frameon=False)
figure = pylab.figure(**parameters)
pylab.title('Figure-reuse Trial')


def render(data):
   timestamp, longitudes, latitudes, values = data['timestamp'], 
data['longitudes'], data['latitudes'], data['values']
   longitudes, latitudes = pylab.meshgrid(longitudes, latitudes)
   pylab.figure(num=1) # set current figure
   parameters = dict(alpha=1, color='white', fontsize=12, fontweight='heavy', 
horizontalalignment='left', verticalalignment='center', 
transform=pylab.gca().transAxes)
   pylab.text(0.01,0.98, timestamp, **parameters)
   x, y = map(longitudes, latitudes)
   map.contourf(x, y, values)
   filename = os.extsep.join(('Reuse-trial',str(data['timestamp'])[:10],'png'))
   pylab.savefig(filename)
   ##pylab.show()
   #~ _restore_(figure) TBD: how to erase current label and contour fill colors 
although the latter erasure surprisingly does not seem to be required !


def timeline():
   ''' a generator for fabricated, planet-wide, data for testing purposes '''
   data = dict()
   delta_x, delta_y   = 1.0, 0.5
   data['longitudes'] = numpy.arange(-180.0, 180.0+delta_x, delta_x)
   data['latitudes']  = numpy.arange(-90.0, 90.0+delta_y, delta_y)
   computations = dict(yesterday=horizontal(data['longitudes'], 
data['latitudes']), today=vertical(data['longitudes'], data['latitudes']))
   for timestamp,values in computations.items():
  data['timestamp'] = timestamp
  data['values']= values
  yield data

def horizontal(longitudes,latitudes):
   data = numpy.empty(shape=(longitudes.size, latitudes.size), dtype=float, 
order='C')
   for index,longitude in enumerate(longitudes):
  data[index] = longitude
   return data
   
def vertical(longitudes,latitudes):
   data = numpy.empty(shape=(longitudes.size, latitudes.size), dtype=float, 
order='C')
   for index,latitude in enumerate(latitudes):
  data[:,index] = latitude
   return data


for data in timeline():
   render(data)
-
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse012070mrt/direct/01/___
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users


[Matplotlib-users] IOError: [Errno 2] No such file or directory: 'etopo20data.gz'

2008-01-14 Thread j. vickroy
Hello, I am a first-time user of matplotlib.

When trying to run the plotmap.py example, the following traceback is 
generated:

 plotmap.py
Traceback (most recent call last):
 File C:\Documents and Settings\jim.vickroy\My 
Documents\Projects\High-latitudes D-region\plotmap.py, line 13, in 
module
   topoin = load('etopo20data.gz')
 File C:\Python25\Lib\site-packages\matplotlib\mlab.py, line 1252, in 
load
   fh = cbook.to_filehandle(fname)
 File C:\Python25\Lib\site-packages\matplotlib\cbook.py, line 236, in 
to_filehandle
   fh = gzip.open(fname, flag)
 File C:\Python25\lib\gzip.py, line 49, in open
   return GzipFile(filename, mode, compresslevel)
 File C:\Python25\lib\gzip.py, line 95, in __init__
   fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
IOError: [Errno 2] No such file or directory: 'etopo20data.gz'

How do I obtain this file and where should it reside?

I have downloaded proj446_win32_bin.zip and unzipped it, but I do not 
see any such file.

I have also attempted to search this user-group archive for references 
to etopo20data.gz but repeatedly get an Unable to connect to Search 
Server error so I do not know what I'm doing incorrectly.

I have installed:
- matplotlib-0.91.2.win32-py2.5.exe
- basemap-0.9.9.1.win32-py2.5.exe
- httplib2-0.4.0.zip
for use with Python 2.5.1.

I apologize if this has been previously addressed.

Thanks,
-- jv

-
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
___
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users