<p dir="auto">This was originally reported as a question on stackoverflow: <a href="http://stackoverflow.com/questions/32707467/reading-png-does-not-give-expected-result" rel="nofollow">http://stackoverflow.com/questions/32707467/reading-png-does-not-give-expected-result</a><br>
The stackoverflow question has a link to a dropbox site that contains the  file <code class="notranslate">W0002_0004.png</code> that triggers a seg. fault when it is read using <code class="notranslate">scipy.misc.imread</code>.</p>
<p dir="auto">I get the seg. fault in both of these setups:</p>
<ul dir="auto">
<li>python 2.7.10, numpy 1.9.2, scipy <code class="notranslate">'0.17.0.dev0+81c0960'</code>, PIL.PILLOW_VERSION 2.5.3</li>
<li>pyhton 3.4.3, numpy 1.9.2, scipy 0.16.0, PIL.PILLOW_VERSION 2.9.0</li>
</ul>
<p dir="auto">The program <code class="notranslate">pngcheck</code> does not report any errors in the file:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ pngcheck -vt W0002_0004.png
File: W0002_0004.png (3721 bytes)
  chunk IHDR at offset 0x0000c, length 13
    4968 x 527 image, 1-bit grayscale, non-interlaced
  chunk tIME at offset 0x00025, length 7:  8 Feb 2015 15:18:29 UTC
  chunk IDAT at offset 0x00038, length 3645
    zlib: deflated, 32K window, default compression
  chunk IEND at offset 0x00e81, length 0
No errors detected in W0002_0004.png (4 chunks, 98.9% compression)."><pre class="notranslate"><code class="notranslate">$ pngcheck -vt W0002_0004.png
File: W0002_0004.png (3721 bytes)
  chunk IHDR at offset 0x0000c, length 13
    4968 x 527 image, 1-bit grayscale, non-interlaced
  chunk tIME at offset 0x00025, length 7:  8 Feb 2015 15:18:29 UTC
  chunk IDAT at offset 0x00038, length 3645
    zlib: deflated, 32K window, default compression
  chunk IEND at offset 0x00e81, length 0
No errors detected in W0002_0004.png (4 chunks, 98.9% compression).
</code></pre></div>
<p dir="auto"><code class="notranslate">imread</code> calls <code class="notranslate">fromimage</code>, and the seg. fault occurs when <code class="notranslate">fromimage</code> calls <code class="notranslate">array(im)</code>.  Here's the sequence of calls equivalent to calling <code class="notranslate">imread('W0002_0004.png')</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [1]: import numpy as np

In [2]: from PIL import Image"><pre class="notranslate"><code class="notranslate">In [1]: import numpy as np

In [2]: from PIL import Image
</code></pre></div>
<p dir="auto"><code class="notranslate">imread</code> uses <code class="notranslate">Image.open</code> to read the file. It then passes <code class="notranslate">im</code> to <code class="notranslate">fromimage</code>.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [3]: im = Image.open('W0002_0004.png')"><pre class="notranslate"><code class="notranslate">In [3]: im = Image.open('W0002_0004.png')
</code></pre></div>
<p dir="auto">If <code class="notranslate">im.mode</code> is <code class="notranslate">'1'</code>, <code class="notranslate">fromimage</code> does <code class="notranslate">im.convert('L')</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [4]: im.mode
Out[4]: '1'

In [5]: im.convert('L')
Out[5]: &lt;PIL.Image.Image image mode=L size=4968x527 at 0x10431E710&gt;"><pre class="notranslate"><code class="notranslate">In [4]: im.mode
Out[4]: '1'

In [5]: im.convert('L')
Out[5]: &lt;PIL.Image.Image image mode=L size=4968x527 at 0x10431E710&gt;
</code></pre></div>
<p dir="auto">Then <code class="notranslate">fromimage</code> returns <code class="notranslate">array(im)</code>.  This is where the seg. fault occurs.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="In [6]: result = np.array(im)
/Users/warren/anaconda/bin/python.app: line 3: 41981 Segmentation fault: 11  /Users/warren/anaconda/python.app/Contents/MacOS/python &quot;$@&quot;"><pre class="notranslate"><code class="notranslate">In [6]: result = np.array(im)
/Users/warren/anaconda/bin/python.app: line 3: 41981 Segmentation fault: 11  /Users/warren/anaconda/python.app/Contents/MacOS/python "$@"
</code></pre></div>
<p dir="auto"><em>Original ticket <a href="http://projects.scipy.org/scipy/ticket/1613" rel="nofollow">http://projects.scipy.org/scipy/ticket/1613</a> on 2012-02-29 by trac user wiredfool, assigned to unknown.</em></p>
<p dir="auto">This is similar to bug 1205, closed as invalid.</p>
<p dir="auto">When trying to open a 1 bit png, I get segfaults.</p>
<p dir="auto">Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)<br>
[GCC 4.4.3] on linux2<br>
Type "help", "copyright", "credits" or "license" for more information.</p>
<blockquote>
<blockquote>
<blockquote>
<p dir="auto">import scipy, numpy<br>
f = scipy.misc.imread('test/horiz_stripe_front.png')<br>
Segmentation fault</p>
</blockquote>
</blockquote>
</blockquote>
<p dir="auto">Opening through PIL, converting to L or RGB, then doing asarray succeeds:</p>
<p dir="auto">Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)<br>
[GCC 4.4.3] on linux2<br>
Type "help", "copyright", "credits" or "license" for more information.</p>
<blockquote>
<blockquote>
<blockquote>
<p dir="auto">import Image<br>
import scipy, numpy<br>
f = Image.open('test/horiz_stripe_front.png')</p>
<p dir="auto">print f.mode<br>
1<br>
front = f.convert("L")<br>
arr = numpy.asarray(front)<br>
type(arr)<br>
&lt;type 'numpy.ndarray'&gt;</p>
</blockquote>
</blockquote>
</blockquote>
<p dir="auto">I've tested in 0.8 (ubuntu oneiric), but I've checked the code and nothing has changed here in head. This is PIL 1.1.7</p>