Check that images are L mode in ImageMorph methods

This commit is contained in:
Andrew Murray 2015-08-28 22:54:17 +10:00
parent bbc6f7d6bb
commit 9026b81439
2 changed files with 18 additions and 1 deletions

View File

@ -198,6 +198,9 @@ class MorphOp(object):
if self.lut is None: if self.lut is None:
raise Exception('No operator loaded') raise Exception('No operator loaded')
if image.mode != 'L':
raise Exception('Image must be binary, meaning it must use mode L')
return
outimage = Image.new(image.mode, image.size, None) outimage = Image.new(image.mode, image.size, None)
count = _imagingmorph.apply( count = _imagingmorph.apply(
bytes(self.lut), image.im.id, outimage.im.id) bytes(self.lut), image.im.id, outimage.im.id)
@ -212,6 +215,9 @@ class MorphOp(object):
if self.lut is None: if self.lut is None:
raise Exception('No operator loaded') raise Exception('No operator loaded')
if image.mode != 'L':
raise Exception('Image must be binary, meaning it must use mode L')
return
return _imagingmorph.match(bytes(self.lut), image.im.id) return _imagingmorph.match(bytes(self.lut), image.im.id)
def get_on_pixels(self, image): def get_on_pixels(self, image):
@ -220,6 +226,9 @@ class MorphOp(object):
Returns a list of tuples of (x,y) coordinates Returns a list of tuples of (x,y) coordinates
of all matching pixels.""" of all matching pixels."""
if image.mode != 'L':
raise Exception('Image must be binary, meaning it must use mode L')
return
return _imagingmorph.get_on_pixels(image.im.id) return _imagingmorph.get_on_pixels(image.im.id)
def load_lut(self, filename): def load_lut(self, filename):

View File

@ -1,5 +1,5 @@
# Test the ImageMorphology functionality # Test the ImageMorphology functionality
from helper import unittest, PillowTestCase from helper import unittest, PillowTestCase, hopper
from PIL import Image from PIL import Image
from PIL import ImageMorph from PIL import ImageMorph
@ -168,6 +168,14 @@ class MorphTests(PillowTestCase):
self.assertEqual(len(coords), 4) self.assertEqual(len(coords), 4)
self.assertEqual(tuple(coords), ((2, 2), (4, 2), (2, 4), (4, 4))) self.assertEqual(tuple(coords), ((2, 2), (4, 2), (2, 4), (4, 4)))
def test_non_binary_images(self):
im = hopper('RGB')
mop = ImageMorph.MorphOp(op_name="erosion8")
self.assertRaises(Exception, lambda: mop.apply(im))
self.assertRaises(Exception, lambda: mop.match(im))
self.assertRaises(Exception, lambda: mop.get_on_pixels(im))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()