Revision: 5306
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=5306&view=rev
Author:   jdh2358
Date:     2008-05-29 13:17:32 -0700 (Thu, 29 May 2008)

Log Message:
-----------
imread via pil now returns lumininance or rgb if possible

Modified Paths:
--------------
    branches/v0_91_maint/API_CHANGES
    branches/v0_91_maint/CHANGELOG
    branches/v0_91_maint/lib/matplotlib/image.py

Modified: branches/v0_91_maint/API_CHANGES
===================================================================
--- branches/v0_91_maint/API_CHANGES    2008-05-29 18:25:58 UTC (rev 5305)
+++ branches/v0_91_maint/API_CHANGES    2008-05-29 20:17:32 UTC (rev 5306)
@@ -1,3 +1,8 @@
+    matplotlib.image.imread now no longer always returns RGBA -- if
+    the image is luminance or RGB, it will return a MxN or MxNx3 array
+    if possible.  Also uint8 is no longer always forced to float.
+
+
 0.91.2 Released
 
     For csv2rec, checkrows=0 is the new default indicating all rows

Modified: branches/v0_91_maint/CHANGELOG
===================================================================
--- branches/v0_91_maint/CHANGELOG      2008-05-29 18:25:58 UTC (rev 5305)
+++ branches/v0_91_maint/CHANGELOG      2008-05-29 20:17:32 UTC (rev 5306)
@@ -1,3 +1,8 @@
+2008-05-29 matplotlib.image.imread now no longer always returns RGBA
+           -- if the image is luminance or RGB, it will return a MxN
+           or MxNx3 array if possible.  Also uint8 is no longer always
+           forced to float.
+
 2008-05-29 Implement path clipping in PS backend - JDH
 
 2008-05-29 Fixed two bugs in texmanager.py:

Modified: branches/v0_91_maint/lib/matplotlib/image.py
===================================================================
--- branches/v0_91_maint/lib/matplotlib/image.py        2008-05-29 18:25:58 UTC 
(rev 5305)
+++ branches/v0_91_maint/lib/matplotlib/image.py        2008-05-29 20:17:32 UTC 
(rev 5306)
@@ -607,11 +607,15 @@
     """
     return image file in fname as numpy array
 
-    Return value is a MxNx4 array of 0-1 normalized floats
+    return value is a numpy array.  For grayscale images, the return
+    array is MxN.  For RGB images, the return value is MxNx3.  For
+    RGBA images the return value is MxNx4
 
     matplotlib can only read PNGs natively, but if PIL is installed,
-    it will use it to load the image and return an RGBA if possible
+    it will use it to load the image and return an array (if possible)
     which can be used with imshow
+
+    TODO: support RGB and grayscale return values in _image.readpng
     """
 
     def pilread():
@@ -639,15 +643,39 @@
 
 
 def pil_to_array( pilImage ):
+    """
+    load a PIL image and return it as a numpy array of uint8.  For
+    grayscale images, the return array is MxN.  For RGB images, the
+    return value is MxNx3.  For RGBA images the return value is MxNx4
+    """
+    def toarray(im):
+        'return a 1D array of floats'
+        x_str = im.tostring('raw',im.mode,0,-1)
+        x = npy.fromstring(x_str,npy.uint8)
+        return x
+
     if pilImage.mode in ('RGBA', 'RGBX'):
-        im = pilImage # no need to convert images in rgba format
+        im = pilImage # no need to convert images
+    elif pilImage.mode=='L':
+        im = pilImage # no need to luminance images
+        # return MxN luminance array
+        x = toarray(im)
+        x.shape = im.size[1], im.size[0]
+        return x
+    elif pilImage.mode=='RGB':
+        #return MxNx3 RGB array
+        im = pilImage # no need to RGB images                
+        x = toarray(im)
+        x.shape = im.size[1], im.size[0], 3
+        return x
+
     else: # try to convert to an rgba image
         try:
             im = pilImage.convert('RGBA')
         except ValueError:
             raise RuntimeError('Unknown image mode')
 
-    x_str = im.tostring('raw',im.mode,0,-1)
-    x = npy.fromstring(x_str,npy.uint8)
+    # return MxNx4 RGBA array
+    x = toarray(im)
     x.shape = im.size[1], im.size[0], 4
     return x


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to