Python crash when downloading image as numpy array
Question: Why does the following code crash python? Is there an easier/better way to
download an image and convert it to a numpy array?

    
    
    from pylab import *
    from urllib import request
    captcha=imread(request.urlopen('http://pastebin.com/etc/CaptchaSecurityImages.php?width=100&height=35&characters=4&b=123'))
    

Note that this causes the python interpreter to exit rather than just printing
a stack trace.


Answer: Several issues:

  1. `imread` can not detect the image type and defaults to `png`.
  2. matplotlib's [_png.read_png crashes on Python 3 with urllib.request object](https://github.com/matplotlib/matplotlib/pull/1650).
  3. the `request.urlopen` object is missing a seek function and does not work with PIL (PIL is used by matplotlib to read non-png images).

This code works for me on win-amd64-py3.3:

    
    
    from pylab import *
    from urllib import request
    from io import BytesIO
    url = 'http://pastebin.com/etc/CaptchaSecurityImages.php?width=100&height=35&characters=4&b=123'
    data = BytesIO(request.urlopen(url).read())
    captcha = imread(data, format='jpg')
    

