Nav*_*vin 2 python numpy urllib scipy python-3.x
为什么以下代码崩溃python?是否有更简单/更好的方法来下载图像并将其转换为numpy数组?
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'))
Run Code Online (Sandbox Code Playgroud)
请注意,这会导致python解释器退出而不仅仅是打印堆栈跟踪.
几个问题:
imread无法检测图像类型和默认值png.request.urlopen对象缺少搜索功能,不能与PIL一起使用(matplotlib使用PIL读取非png图像).这段代码适用于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')
Run Code Online (Sandbox Code Playgroud)