[Scipy-svn] r3273 - in trunk/scipy/misc: . tests tests/data

scipy-svn@scip... scipy-svn@scip...
Tue Aug 28 11:55:00 CDT 2007


Author: stefan
Date: 2007-08-28 11:54:29 -0500 (Tue, 28 Aug 2007)
New Revision: 3273

Added:
   trunk/scipy/misc/tests/data/
   trunk/scipy/misc/tests/data/icon.png
   trunk/scipy/misc/tests/data/icon_mono.png
   trunk/scipy/misc/tests/data/icon_mono_flat.png
Modified:
   trunk/scipy/misc/pilutil.py
   trunk/scipy/misc/tests/test_pilutil.py
Log:
Fix fromimage for monochrome images (closes ticket #259).  Add tests.


Modified: trunk/scipy/misc/pilutil.py
===================================================================
--- trunk/scipy/misc/pilutil.py	2007-08-28 16:29:41 UTC (rev 3272)
+++ trunk/scipy/misc/pilutil.py	2007-08-28 16:54:29 UTC (rev 3273)
@@ -46,53 +46,26 @@
     return
 
 def fromimage(im, flatten=0):
-    """Takes a PIL image and returns a copy of the image in a numpy container.
-    If the image is RGB returns a 3-dimensional array:  arr[:,:,n] is each channel
+    """Return a copy of a PIL image as a numpy array.
 
-    Optional arguments:
+    :Parameters:
+        im : PIL image
+            Input image.
+        flatten : bool
+            If true, convert the output to grey-scale.
 
-    - flatten (0): if true, the image is flattened by calling convert('F') on
-    the image object before extracting the numerical data.  This flattens the
-    color layers into a single grayscale layer.  Note that the supplied image
-    object is NOT modified.
+    :Returns:
+        img_array : ndarray
+            The different colour bands/channels are stored in the
+            third dimension, such that a grey-image is MxN, an
+            RGB-image MxNx3 and an RGBA-image MxNx4.
+
     """
-    assert Image.isImageType(im), "Not a PIL image."
+    if not Image.isImageType(im):
+        raise TypeError("Input is not a PIL image.")
     if flatten:
         im = im.convert('F')
-    mode = im.mode
-    adjust = 0
-    if mode == '1':
-        im = im.convert(mode='L')
-        mode = 'L'
-        adjust = 1
-    str = im.tostring()
-    type = uint8
-    if mode == 'F':
-        type = numpy.float32
-    elif mode == 'I':
-        type = numpy.uint32
-    elif mode == 'I;16':
-        type = numpy.uint16
-    arr = numpy.fromstring(str,type)
-    shape = list(im.size)
-    shape.reverse()
-    if mode == 'P':
-        arr.shape = shape
-        if im.palette.rawmode != 'RGB':
-            print "Warning: Image has invalid palette."
-            return arr
-        pal = numpy.fromstring(im.palette.data,type)
-        N = len(pal)
-        pal.shape = (int(N/3.0),3)
-        return arr, pal
-    if mode in ['RGB','YCbCr']:
-        shape += [3]
-    elif mode in ['CMYK','RGBA']:
-        shape += [4]
-    arr.shape = shape
-    if adjust:
-        arr = (arr != 0)
-    return arr
+    return array(im)
 
 _errstr = "Mode is unknown or incompatible with input array shape."
 def toimage(arr,high=255,low=0,cmin=None,cmax=None,pal=None,

Added: trunk/scipy/misc/tests/data/icon.png
===================================================================
(Binary files differ)


Property changes on: trunk/scipy/misc/tests/data/icon.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/scipy/misc/tests/data/icon_mono.png
===================================================================
(Binary files differ)


Property changes on: trunk/scipy/misc/tests/data/icon_mono.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Added: trunk/scipy/misc/tests/data/icon_mono_flat.png
===================================================================
(Binary files differ)


Property changes on: trunk/scipy/misc/tests/data/icon_mono_flat.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Modified: trunk/scipy/misc/tests/test_pilutil.py
===================================================================
--- trunk/scipy/misc/tests/test_pilutil.py	2007-08-28 16:29:41 UTC (rev 3272)
+++ trunk/scipy/misc/tests/test_pilutil.py	2007-08-28 16:54:29 UTC (rev 3273)
@@ -1,22 +1,41 @@
 from numpy.testing import *
 set_package_path()
+import PIL.Image
 import scipy.misc.pilutil as pilutil
 restore_path()
 
+import glob
+import os.path
 import numpy as N
 
-class test_pilutil(NumpyTestCase):
-    def check_imresize(self):
+datapath = os.path.dirname(__file__)
+
+class test_pilutil(ParametricTestCase):
+    def test_imresize(self):
         im = N.random.random((10,20))
         for T in N.sctypes['float'] + [float]:
             im1 = pilutil.imresize(im,T(1.1))
             assert_equal(im1.shape,(11,22))
 
-    def check_bytescale(self):
+    def test_bytescale(self):
         x = N.array([0,1,2],N.uint8)
         y = N.array([0,1,2])
         assert_equal(pilutil.bytescale(x),x)
         assert_equal(pilutil.bytescale(y),[0,127,255])
 
+    def tst_fromimage(self,filename,irange):
+        img = pilutil.fromimage(PIL.Image.open(filename))
+        imin,imax = irange
+        assert img.min() >= imin
+        assert img.max() <= imax
+
+    def testip_fromimage(self):
+        data = {'icon.png':(0,255),
+                'icon_mono.png':(0,2),
+                'icon_mono_flat.png':(0,1)}
+
+        return ((self.tst_fromimage,os.path.join(datapath,'data',fn),irange)
+                for fn,irange in data.iteritems())
+
 if __name__ == "__main__":
     NumpyTest().run()



More information about the Scipy-svn mailing list