2

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.

2
  • It doesn't crash: NameError: name 'imread' is not defined. Commented Mar 3, 2013 at 6:52
  • @Bakuriu You have to import pylab Commented Mar 3, 2013 at 6:53

1 Answer 1

7

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.
  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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.