Pillow: ImageMorph.MorphOp() crashes Python when applied to a non-grayscale image

Created on 28 Aug 2015  路  6Comments  路  Source: python-pillow/Pillow

python 3.4.3 from macports
pillow34 from macports
pycharm 4.5.2
osx 10.10 (yosemite)

from PIL import ImageMorph
from PIL import Image
from PIL import ImageOps

a_path = "......."
a = Image.open(a_path)

#need to do this because pillow is lazy sometimes
a.load() 

#if you don't do this, ImageMorph will crash horribly
#a = ImageOps.grayscale(a) 

erode = ImageMorph.MorphOp(op_name="erosion8")

# we never get here because python is dead
(n_pix_diff, new_a) = erode.apply(a)
Bug

Most helpful comment

Now that it's fixed I'll say it: This was my first-ever contribution to an open-source project and wow it feels good!

Thank you both for making my day! Bug reports are the gateway drug methinks.

All 6 comments

Reproducible with hopper.png and:
Python 2.7.9
Pillow 2.9.0
OS X 10.10 (Yosemite)

Python 2.7.9 (default, Dec 19 2014, 06:05:48) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from PIL import ImageMorph
>>> from PIL import Image
>>> from PIL import ImageOps
>>> a_path = "hopper.png"
>>> a = Image.open(a_path)
>>> a.load() 
<PixelAccess object at 0x10bb11190>
>>> erode = ImageMorph.MorphOp(op_name="erosion8")
>>> (n_pix_diff, new_a) = erode.apply(a)
Segmentation fault: 11

Here's part of the problem report:

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_INVALID_ADDRESS at 0x0000000000000000

VM Regions Near 0:
--> 
    __TEXT                 000000010b916000-000000010b918000 [    8K] r-x/rwx SM=COW  /usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   _imagingmorph.so                0x000000010bed3924 apply + 292
1   org.python.python               0x000000010b9a27e6 PyEval_EvalFrameEx + 14392
2   org.python.python               0x000000010b9a562e fast_function + 262
3   org.python.python               0x000000010b9a2400 PyEval_EvalFrameEx + 13394
4   org.python.python               0x000000010b99ed7a PyEval_EvalCodeEx + 1409
5   org.python.python               0x000000010b99e7f3 PyEval_EvalCode + 54
6   org.python.python               0x000000010b9be8a2 run_mod + 53
7   org.python.python               0x000000010b9be6be PyRun_InteractiveOneFlags + 353
8   org.python.python               0x000000010b9be1cd PyRun_InteractiveLoopFlags + 192
9   org.python.python               0x000000010b9be077 PyRun_AnyFileExFlags + 60
10  org.python.python               0x000000010b9cfc5b Py_Main + 3051
11  libdyld.dylib                   0x00007fff9ca9e5c9 start + 1

The original PR for this feature says:

This pull request contains an initial implementation of a binary mathematical morphology (see: http://en.wikipedia.org/wiki/Mathematical_morphology) addon for Pillow. It is used to apply morphological operators on PIL images of mode 'L' where each pixel is considered binary.

And the tests only use binary images.

It would make sense for Pillow to give an error message in this case instead of crashing.

So PR #1400?

PR merged, thanks both!

Now that it's fixed I'll say it: This was my first-ever contribution to an open-source project and wow it feels good!

Thank you both for making my day! Bug reports are the gateway drug methinks.

@jdanielp :+1: And thanks for the good report!

Was this page helpful?
0 / 5 - 0 ratings