diff --git a/PIL/Image.py b/PIL/Image.py index 49c9f6ab2..335f1c1ad 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1954,9 +1954,7 @@ class Image(object): if self.im.bands == 1: ims = [self.copy()] else: - ims = [] - for i in range(self.im.bands): - ims.append(self._new(self.im.getband(i))) + ims = map(self._new, self.im.split()) return tuple(ims) def tell(self): diff --git a/_imaging.c b/_imaging.c index 7b3380b40..a87ae67d1 100644 --- a/_imaging.c +++ b/_imaging.c @@ -1905,6 +1905,32 @@ _putband(ImagingObject* self, PyObject* args) return Py_None; } +static PyObject* +_split(ImagingObject* self, PyObject* args) +{ + int fails = 0; + Py_ssize_t i; + PyObject* list; + PyObject* imaging_object; + Imaging bands[4] = {NULL, NULL, NULL, NULL}; + + if ( ! ImagingSplit(self->image, bands)) + return NULL; + + list = PyTuple_New(self->image->bands); + for (i = 0; i < self->image->bands; i++) { + imaging_object = PyImagingNew(bands[i]); + if ( ! imaging_object) + fails += 1; + PyTuple_SET_ITEM(list, i, imaging_object); + } + if (fails) { + Py_DECREF(list); + list = NULL; + } + return list; +} + /* -------------------------------------------------------------------- */ #ifdef WITH_IMAGECHOPS @@ -2972,6 +2998,7 @@ static struct PyMethodDef methods[] = { {"getband", (PyCFunction)_getband, 1}, {"putband", (PyCFunction)_putband, 1}, + {"split", (PyCFunction)_split, 1}, {"fillband", (PyCFunction)_fillband, 1}, {"setmode", (PyCFunction)im_setmode, 1}, diff --git a/libImaging/Bands.c b/libImaging/Bands.c index c1e20d8aa..9614ebd2c 100644 --- a/libImaging/Bands.c +++ b/libImaging/Bands.c @@ -72,6 +72,19 @@ ImagingGetBand(Imaging imIn, int band) return imOut; } + +int +ImagingSplit(Imaging imIn, Imaging bands[4]) +{ + int i; + for (i = 0; i < imIn->bands; i++) { + bands[i] = ImagingNew("L", imIn->xsize, imIn->ysize); + } + + return imIn->bands; +} + + Imaging ImagingPutBand(Imaging imOut, Imaging imIn, int band) { diff --git a/libImaging/Imaging.h b/libImaging/Imaging.h index 99fff7f67..9386fc260 100644 --- a/libImaging/Imaging.h +++ b/libImaging/Imaging.h @@ -267,6 +267,7 @@ extern Imaging ImagingFlipTopBottom(Imaging imOut, Imaging imIn); extern Imaging ImagingGaussianBlur(Imaging imOut, Imaging imIn, float radius, int passes); extern Imaging ImagingGetBand(Imaging im, int band); +extern int ImagingSplit(Imaging im, Imaging bands[4]); extern int ImagingGetBBox(Imaging im, int bbox[4]); typedef struct { int x, y; INT32 count; INT32 pixel; } ImagingColorItem; extern ImagingColorItem* ImagingGetColors(Imaging im, int maxcolors,