From ff02f4ce9d55241da96072fc326271a893d88f67 Mon Sep 17 00:00:00 2001 From: Luis Pedro Coelho Date: Fri, 12 Nov 2010 09:44:58 -0500 Subject: [PATCH] BUG: Correctly handle images with an alpha channel Bug caught by Zachary Pincus on scipy-user. --- scikits/image/io/_plugins/freeimage_plugin.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/scikits/image/io/_plugins/freeimage_plugin.py b/scikits/image/io/_plugins/freeimage_plugin.py index 4e71312b..495c75e3 100644 --- a/scikits/image/io/_plugins/freeimage_plugin.py +++ b/scikits/image/io/_plugins/freeimage_plugin.py @@ -320,10 +320,17 @@ def _array_from_bitmap(bitmap): return arr[..., ::-1].T if len(shape) == 3 and _FI.FreeImage_IsLittleEndian() and \ dtype.type == numpy.uint8: - b = array[0] - g = array[1] - r = array[2] - return numpy.dstack( (n(r), n(g), n(b)) ) + b = n(array[0]) + g = n(array[1]) + r = n(array[2]) + if shape[0] == 3: + return numpy.dstack( (r, g, b) ) + elif shape[0] == 4: + a = n(array[3]) + return numpy.dstack( (r, g, b, a) ) + else: + raise ValueError('Cannot handle images of shape %s' % shape) + # We need to copy because array does *not* own its memory # after bitmap is freed. return n(array).copy()