From f5c68531ab11891bdc3a221894438eb8eea83ac4 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Tue, 22 Oct 2013 13:07:16 -0400 Subject: [PATCH] Novice module can open images from a url --- skimage/novice/_novice.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/skimage/novice/_novice.py b/skimage/novice/_novice.py index 2fc4b280..fbae0768 100644 --- a/skimage/novice/_novice.py +++ b/skimage/novice/_novice.py @@ -1,6 +1,10 @@ import os import imghdr +import shutil from collections import namedtuple +from io import BytesIO +from urlparse import urlparse +from urllib2 import urlopen import numpy as np from skimage import io @@ -19,7 +23,7 @@ colors = namedtuple('colors', color_dict.keys())(**color_dict) def open(path): """Return Picture object from the given image path.""" - return Picture(path=os.path.abspath(path)) + return Picture(path=path) def _verify_picture_index(index): @@ -212,7 +216,16 @@ class Picture(object): elif path is not None: self.array = img_as_ubyte(io.imread(path)) self._path = path - self._format = imghdr.what(path) + + # Copy to in-memory buffer to get format (in case path was a url) + if urlparse(path).scheme == "": + self._format = imghdr.what(path) + else: + data = BytesIO() + shutil.copyfileobj(urlopen(path), data) + data.seek(0) + self._format = imghdr.what(data) + del data elif array is not None: self.array = array elif xy_array is not None: